在QR码扫描时拍照

时间:2013-08-03 12:51:42

标签: java android photo zxing scanning

我有一个zxing集成的应用程序。我一直在寻找在扫描QR码时试图存储照片。肖恩欧文建议如下:

  

“该应用程序正在从相机中获取连续的帧流以进行分析。您可以通过在预览回调中拦截它们来存储它们中的任何一个。”

据我所知,预览回调的唯一实例是在CameraManager.java活动(https://code.google.com/p/zxing/source/browse/trunk/android/src/com/google/zxing/client/android/camera/CameraManager.java)内。

特别是:

public synchronized void requestPreviewFrame(Handler handler, int message) {
Camera theCamera = camera;
if (theCamera != null && previewing) {
  previewCallback.setHandler(handler, message);
  theCamera.setOneShotPreviewCallback(previewCallback);
}}

由于每个帧都运行,因此我没有保存任何特定帧的方法(最好是字节日期)。我会假设有一点将某些东西传递回CaptureActivity.java类(底部给出的链接)但是我自己没有找到任何东西。

任何使用过Zxing的人都会知道扫描后在扫描数据的屏幕上显示一个幽灵般的图像,如果有可能劫持这部分代码并将该数据转换和/或保存为可能的字节代码也很有用。

任何帮助或其他想法都将非常感激。对任何进一步信息的请求将很快得到回复。谢谢。

此文件夹中提供完整代码:https://code.google.com/p/zxing/source/browse/trunk#trunk%2Fandroid%2Fsrc%2Fcom%2Fgoogle%2Fzxing%2Fclient%2Fandroid

更新

到目前为止,以下代码段似乎是保存字节数据的可能位置,两者都在DecodeHandler.java类中。

private void decode(byte[] data, int width, int height) {
long start = System.currentTimeMillis();
Result rawResult = null;
PlanarYUVLuminanceSource source = activity.getCameraManager().buildLuminanceSource(data, width, height);
if (source != null) {
  BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));

  //here?

  try {
    rawResult = multiFormatReader.decodeWithState(bitmap);
  } catch (ReaderException re) {
    // continue
  } finally {
    multiFormatReader.reset();
  }
}

Handler handler = activity.getHandler();
if (rawResult != null) {
  // Don't log the barcode contents for security.
  long end = System.currentTimeMillis();
  Log.d(TAG, "Found barcode in " + (end - start) + " ms");
  if (handler != null) {
    Message message = Message.obtain(handler, R.id.decode_succeeded, rawResult);
    Bundle bundle = new Bundle();
    Bitmap grayscaleBitmap = toBitmap(source, source.renderCroppedGreyscaleBitmap());

    //I believe this bitmap is the one shown on screen after a scan has been performed

    bundle.putParcelable(DecodeThread.BARCODE_BITMAP, grayscaleBitmap);
    message.setData(bundle);
    message.sendToTarget();
  }
} else {
  if (handler != null) {
    Message message = Message.obtain(handler, R.id.decode_failed);
    message.sendToTarget();
  }
}}


  private static Bitmap toBitmap(LuminanceSource source, int[] pixels) {
int width = source.getWidth();
int height = source.getHeight();
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);

//saving the bitmnap at this point or slightly sooner, before grey scaling could work.

return bitmap;}

更新:在PreviewCallback.java中找到请求的代码

public void onPreviewFrame(byte[] data, Camera camera) {
Point cameraResolution = configManager.getCameraResolution();
Handler thePreviewHandler = previewHandler;
if (cameraResolution != null && thePreviewHandler != null) {
  Message message = thePreviewHandler.obtainMessage(previewMessage, cameraResolution.x,
      cameraResolution.y, data);
  message.sendToTarget();
  previewHandler = null;
} else {
  Log.d(TAG, "Got preview callback, but no handler or resolution available");
}

1 个答案:

答案 0 :(得分:2)

预览回调的数据是NV21格式。所以如果你想保存它,你可以使用这样的代码:

YuvImage im = new YuvImage(byteArray, ImageFormat.NV21, width,
                        height, null);
            Rect r = new Rect(0, 0, width, height);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            im.compressToJpeg(r, 50, baos);

            try {
                  FileOutputStream output = new FileOutputStream("/sdcard/test_jpg.jpg");
                  output.write(baos.toByteArray());
                  output.flush();
                  output.close();
            } catch (FileNotFoundException e) {
            } catch (IOException e) {
            }

保存点是ZXing可以解码byte []并成功返回内容字符串。