Google条形码API,保存图片

Google Barcode API, Save image

所以我一直在尝试根据 Google 制作的条形码 API 制作一个应用程序并添加一些调整。我是一名学生,还在学习,所以我不知道下一步该怎么做,也许你能给我指明正确的方向。

目前我有

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;
        }
    }

但除了取出条形码外,我还想保存整个图像,我尝试通过实现:mCameraSource.takePicture(null,null);

我该怎么办?

拍照方式:

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);
        }
    }
}

the documentation 中所述,takePicture 方法有 2 个参数。都是a callback.

回调是接口的实现,其中的方法将在某个时候由提供它的方法调用。 通常你在方法异步时使用回调,因为你不知道什么时候会得到结果并且你不能阻止当前线程等待结果(否则进行异步调用毫无意义)。

例子

你有一个接口:

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

还有一个方法:

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

在某些时候,该方法将调用 onResult 回调

public void testMethod(Callback callback){
   // A lot of asynchronous calculation
   // ...

  callback.onResult(value);
}

因此,当您调用方法时:

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

或者您可以在变量中设置回调:

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

然后传给方法:

testMethod(cb);

或者甚至创建一个 class 实现接口(如果它是复杂的东西)。

我们的案例

所以基本上 takePicture 的第二个回调有一个方法,该方法的参数是 "JPEG image data"。所以你需要做的就是实现回调来获取数据,然后你就可以用它们做任何你想做的事情了。例如,您可以存储它们,将它们发送到 API...

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