Android ZXing Get Barcode Image

冷暖自知 提交于 2019-12-29 14:20:55

问题


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


回答1:


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")



回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/11697001/android-zxing-get-barcode-image

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!