18

I am using Zxing library to generate a barcode in my Android application

Intent intent = new Intent("com.google.zxing.client.android.ENCODE");

intent.putExtra("ENCODE_FORMAT", "UPC_A");
intent.putExtra("ENCODE_DATA", "55555555555");

startActivityForResult(intent,0);

Is there anyway to save the generated image in my application which is calling Zxing? I see that in my onActivityResult I get intent null.

Thanks in advance for your help

HT03
  • 320
  • 1
  • 3
  • 10
  • Have you solved this issue? I have the same one. – Stas Aug 27 '12 at 11:18
  • I have the same issue too. I would like to extract the generated image, probably from the onActivityResult like you said... – Karim Apr 20 '13 at 16:44

3 Answers3

37

Take the views cache and save it in bitmap something like this

View myBarCodeView = view.getRootView()
//Else this might return null
myBarCodeView.setDrawingCacheEnabled(true)
//Save it in bitmap
Bitmap mBitmap = myBarCodeView.getDrawingCache()

OR draw your own barcode or QR CODE

//Change the writers as per your need
private void generateQRCode(String data) {
    com.google.zxing.Writer writer = new QRCodeWriter();
    String finaldata =Uri.encode(data, "ISO-8859-1");
    try {
        BitMatrix bm = writer.encode(finaldata,BarcodeFormat.QR_CODE, 350, 350);
        mBitmap = Bitmap.createBitmap(350, 350, Config.ARGB_8888);
        for (int i = 0; i < 350; i++) {
            for (int j = 0; j < 350; j++) {
                mBitmap.setPixel(i, j, bm.get(i, j) ? Color.BLACK: Color.WHITE);
            }
        }
    } catch (WriterException e) {
        e.printStackTrace();
    }
    if (mBitmap != null) {
        mImageView.setImageBitmap(mBitmap);
    }
}
public void generateBarCode(String data){
    com.google.zxing.Writer c9 = new Code128Writer();
    try {
        BitMatrix bm = c9.encode(data,BarcodeFormat.CODE_128,350, 350);
        mBitmap = Bitmap.createBitmap(350, 350, Config.ARGB_8888);

        for (int i = 0; i < 350; i++) {
            for (int j = 0; j < 350; j++) {

                mBitmap.setPixel(i, j, bm.get(i, j) ? Color.BLACK : Color.WHITE);
            }
        }
    } catch (WriterException e) {
        e.printStackTrace();
    }
    if (mBitmap != null) {
        mImageView.setImageBitmap(mBitmap);
    }
}

Once you get the bitmap image just save it

//create a file to write bitmap data
    File f = new File(FilePath, FileName+".png");
    f.createNewFile();

    //Convert bitmap to byte array
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ImageBitmap.compress(CompressFormat.PNG, 0, bos);
    byte[] bytearray = bos.toByteArray();

    //Write bytes in file
    FileOutputStream fos = new FileOutputStream(f);
    fos.write(bytearray);
    fos.flush();
    fos.close();

You can also check a small library from github that i had created to create Barcode or QR Code

GZxingEncoder   Encoder = GZxingEncoder.getInstance();
Encoder.initalize(this);
//To generate bar code use this
Bitmap bitmap = Encoder.generateBarCode_general("some text")
Girish Nair
  • 5,148
  • 5
  • 40
  • 61
  • 1
    The first snippet is not going to work. You can't grab a 'screenshot' of another Activity's view. The rest is not a bad idea.. just embed the encoding. – Sean Owen Apr 20 '13 at 16:51
  • @Girish Nair i m scanning barcode and qrcode both then how will i check either i have to generate barcode or qrcode – Erum Feb 04 '15 at 19:43
  • @ErumHannan : Bar Code & QR Code are some what like 2 different languages, so what you choose to speak is your choice. So how about an Alert Dialog asking the user weather he/she wants to generate a Bar Code or QR Code – Girish Nair Feb 05 '15 at 16:03
1

It is not returned in the Intent right now. There's no way to get it. You could suggest a patch to make it be returned -- it is probably a couple days' work. Or try Girish's approach, which is just to embed the encoding directly.

Sean Owen
  • 66,182
  • 23
  • 141
  • 173
  • Okay! Thanks for your fast answer :) Like I said, I have to use another app. I am trying to build something around intents for research purposes, so I will suggest a patch. Can you please tell me how/where to suggest a patch? – Karim Apr 20 '13 at 16:56
  • Sure, the `EncoderActivity` is here: https://code.google.com/p/zxing/source/browse/trunk#trunk%2Fandroid%2Fsrc%2Fcom%2Fgoogle%2Fzxing%2Fclient%2Fandroid%2Fencode It would have to return some compressed binary representation in the `Intent`, which is then processed here: https://code.google.com/p/zxing/source/browse/trunk/android-integration/src/com/google/zxing/integration/android/IntentIntegrator.java You can make a patch against SVN ideally. – Sean Owen Apr 20 '13 at 18:11
1

To store the scanned image in ZXing, You have to override a method drawResultPoints in Class CaptureActivity.

 String root = Environment.getExternalStorageDirectory().toString();
 File myDir = new File(root);    
 myDir.mkdirs();
 Random generator = new Random();
 int n = 10000;
 n = generator.nextInt(n);
 String fname = "Image-"+ n +".jpg";
 File file = new File (myDir, fname);
 if (file.exists ()) file.delete (); 
 try {
     FileOutputStream out = new FileOutputStream(file);
     barcode.compress(Bitmap.CompressFormat.JPEG, 90, out);
     out.flush();
     out.close();

 } catch (Exception e) {
   Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
 }

This will saved the scanned image in the root directory of SD card, you can customize it to save it in any particular folder you need. The image it will be storing is the scanned image which appears as a ghost image while you scan.

Rakshi
  • 6,796
  • 3
  • 25
  • 46
  • Actually this is really useful. Yes, you can steal the drawing cache. But this is a more reliable method of getting the scanned bitmap. – Eran Goldin Sep 09 '14 at 09:47
  • @Rakshi can u explain more – Erum Feb 05 '15 at 17:00
  • @EranGoldin yes this method is working fine for saving image but how will i get the name of image ? inside onActivityResult – Erum Feb 05 '15 at 18:29
  • @ErumHanan name? You are acquiring a bitmap, it has no name. – Eran Goldin Feb 06 '15 at 02:17
  • Sorry but how does this work? I'm trying to extend `CaptureActivity` instead of `Activity`, but I can't extend `CaptureActivity`. Using it in implements does not work as well. I can't override a `drawResultPoints` function. Where can I find the `CaptureActivity` class? – Razgriz Dec 04 '15 at 14:01