-1

i am working on an application that involves the scanning of barcodes. So far, i have successfully get scanned the barcodes using camera preview by following the below. link https://github.com/ZBar/ZBar

However, the way it works does not really meet my needs.The example uses a camera preview. However on my project application, i have an application that allowus users to access the camera via the click of a button(Intent).

After that it converts the captured images to byte array.

Is there anyway to use enable Zbar to scan images(byte arrays) instead? Or is there anyway to use Zbar with android's camera instead of camera preview ?

Thank You.

This is my MainActivity.java which allows users to access the camera via intent.

public class MainActivity extends Activity {

Button cameraBtn;
final int REQUEST_IMAGE_CAPTURE = 1;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    cameraBtn = (Button)findViewById(R.id.cameraBtn);

    cameraBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(cameraIntent, REQUEST_IMAGE_CAPTURE);
        }
    });
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
        Bundle extras = data.getExtras();
        Bitmap imageBitmap = (Bitmap) extras.get("data");
    }
}}
OneTwoThree
  • 11
  • 1
  • 3

1 Answers1

0

Zbar libs makes your work easier by inheriting all the camera functionalities. However, you can still create your own Camera/CameraPreview to scan barcodes using Zbar api's.

Below code snippets explains how Zbar scans your barcode for each frame. onPreviewFrame callback gives you each frame captured by your camera.

public void onPreviewFrame(byte[] data, Camera camera) {
            Camera.Parameters parameters = camera.getParameters();
            Size size = parameters.getPreviewSize();

            Image barcode = new Image(size.width, size.height, "Y800");
            barcode.setData(data);

            int result = scanner.scanImage(barcode);
            if (result != 0) {
                SymbolSet syms = scanner.getResults();
                for (Symbol sym : syms) {
                    Log.v("", sym.getData());
                }
            }
}

Here, you need to create your own camera activity which overrides onPreviewFrame callabck. Also, you need to load iconv library(part of Zlib), instantiating Scanner instance.

static {
    System.loadLibrary("iconv");
} 

ImageScanner scanner = new ImageScanner();
scanner.setConfig(0, Config.X_DENSITY, 3);
scanner.setConfig(0, Config.Y_DENSITY, 3);
Daud Arfin
  • 2,499
  • 1
  • 18
  • 37