Search code examples
javaandroidapibarcodebarcode-scanner

Google Barcode API, Save image


So i've been trying to make an application based on the Barcode API made by Google and adding some tweaks. I'm a student and still learning, so i don't have a clue on what to do next, maybe you could point me in the right direction.

Currently i have

private boolean onTap(float rawX, float rawY) {
    mCameraSource.takePicture(null,null);

    // Find tap point in preview frame coordinates.
    int[] location = new int[2];
    mGraphicOverlay.getLocationOnScreen(location);
    float x = (rawX - location[0]) / mGraphicOverlay.getWidthScaleFactor();
    float y = (rawY - location[1]) / mGraphicOverlay.getHeightScaleFactor();

    // Find the barcode whose center is closest to the tapped point.
    Barcode best = null;
    float bestDistance = Float.MAX_VALUE;
    for (BarcodeGraphic graphic : mGraphicOverlay.getGraphics()) {
        Barcode barcode = graphic.getBarcode();
        if (barcode.getBoundingBox().contains((int) x, (int) y)) {
            // Exact hit, no need to keep looking.
            best = barcode;
            break;
        }
        float dx = x - barcode.getBoundingBox().centerX();
        float dy = y - barcode.getBoundingBox().centerY();
        float distance = (dx * dx) + (dy * dy);  // actually squared distance
        if (distance < bestDistance) {
            best = barcode;
            bestDistance = distance;
        }
    }

But besides taking out the barcode, i want to save the entire image as well which i tried by implementing: mCameraSource.takePicture(null,null);

What do i do?

Takepicture method:

public void takePicture(ShutterCallback shutter, PictureCallback jpeg) {
    synchronized (mCameraLock) {
        if (mCamera != null) {
            PictureStartCallback startCallback = new PictureStartCallback();
            startCallback.mDelegate = shutter;
            PictureDoneCallback doneCallback = new PictureDoneCallback();
            doneCallback.mDelegate = jpeg;
            mCamera.takePicture(startCallback, null, null, doneCallback);
        }
    }
}

Solution

  • As explained in the documentation the takePicture method has 2 parameters. Both are a callback.

    A callback is the implementation of an interface, where the methods will be called at some point by the method where it's provided. Generally you use a callback when the method is asynchronous as you don't know when you'll get the result and you can't block your current Thread to wait for the result (otherwise it's pointless to do an asynchronous call).

    Example

    You have an interface:

    public interface Callback{
       public void onResult(int value);
    }
    

    And a method:

    public void testMethod(Callback callback){
       // Do something
    }
    

    At some point the method will call onResult on the callback

    public void testMethod(Callback callback){
       // A lot of asynchronous calculation
       // ...
    
      callback.onResult(value);
    }
    

    So when you call the method you do:

    testMethod(new Callback(){
       @Override
       public void onResult(int value){
         // Do something with the result;
       }
    });
    

    Or you can set the callback in a variable:

    Callback cb = new Callback(){
     @Override
     public void onResult(int value){
         // Do something with the result;
       }
    };
    

    and then pass it to the method:

    testMethod(cb);
    

    Or even create a class which implement the interface (if it's something complexe).

    Our case

    So basically the second callback of takePicture has method which has as parameter the "JPEG image data". So all you need to do is to implement the callback to get the data and then you can do whatever you do with them. For example you can store them, send them to an API...

     mCameraSource.takePicture(null, new CameraSource.PictureCallback(){
       @Override
        public  void onPictureTaken (byte[] data){
           // Do something with the data (this is the JPEG image)
       }
    });