java - 扫描二维码拍照

标签 java android photo zxing scanning

我有一个集成了 zxing 的应用程序。我一直在考虑尝试在扫描二维码时存储照片。肖恩欧文推荐以下内容:

"The app is getting a continuous stream of frames from the camera to analyze. You can store off any of them by intercepting them in the preview callback."

据我所知,预览回调的唯一实例是在 CameraManager.java Activity ( 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");
}

最佳答案

预览回调的数据为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[]并成功返回content String。

关于java - 扫描二维码拍照,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18033066/

相关文章:

java - Hibernate (mysql) 为同一个查询返回不同的结果

java - Swing 应用程序问题中的窗口关闭

android - OpenGL ES 2.0 手机是否也包含 1.x?

java - Android 处理没有互联网连接的物联网设备

algorithm - 快速获取图像主色的方法

java - org.openqa.selenium.SessionNotCreatedException:无法创建新的远程 session 。在模拟器中初始化android驱动程序时

java - 使用 Long 类型的对象参数对对象列表进行排序

android - 增加android中的网格间距

algorithm - 如何用实数坐标计算像素的颜色

c# - 从照片中获取 Exif 信息