0

在过去的几周里,我一直在尝试改变 Zxing 以在扫描后立即拍照。感谢帮助,我可以始终如一地从 PreviewCallback.java 中的 onPreviewFrame 类保存图像

下面是我在 onPreviewMethod 方法中使用的代码,然后简要介绍了我的应用程序的工作原理。

public void onPreviewFrame(byte[] data, Camera camera) {
Point cameraResolution = configManager.getCameraResolution();
Handler thePreviewHandler = previewHandler;

android.hardware.Camera.Parameters parameters = camera.getParameters();
android.hardware.Camera.Size size = parameters.getPreviewSize();

int height = size.height;
int width = size.width;

System.out.println("HEIGHT IS" + height);
System.out.println("WIDTH IS" + width);



if (cameraResolution != null && thePreviewHandler != null) {


    YuvImage im = new YuvImage(data, 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();

                System.out.println("Attempting to save file");
                System.out.println(data);

            } catch (FileNotFoundException e) {

                System.out.println("Saving to file failed");

            } catch (IOException e) {

                System.out.println("Saving to file failed");

            }


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

我的应用程序以自己的 GUI 和功能为中心,但可以通过 Intent 使用 Zxing(Zxing 内置在应用程序构建路径中,是的,这很糟糕,因为如果已经安装了 Zxing,它可能会干扰)。一旦 Zxing 扫描了一个二维码,上面编码的信息就会返回到我的应用程序并存储,然后在短暂的延迟后自动重新启动 Zxing。

我当前的代码在 Zxing 运行时每帧保存一张图像,我想要的功能是只保存扫描时的帧。尽管 Zxing 在我的应用程序再次接管的短窗口中停止保存图像,但是 Zxing 很快重新初始化,我可能没有时间处理数据。然而,一个可能的解决方法是快速重命名保存的文件,这样 Zxing 就不会开始覆盖它,并且可以在后台执行操作。然而,每帧保存一张图像是一种资源浪费,而且不太可取。

如何仅在扫描时保存图像?

提前致谢。


根据要求更新以显示找到的 multiFormatReader 实例:

private final CaptureActivity activity;
private final MultiFormatReader multiFormatReader;
private boolean running = true;
DecodeHandler(CaptureActivity activity, Map<DecodeHintType,Object> hints) {
multiFormatReader = new MultiFormatReader();
multiFormatReader.setHints(hints);
this.activity = activity;
}
@Override
public void handleMessage(Message message) {
if (!running) {
  return;
}
if (message.what == R.id.decode) {
    decode((byte[]) message.obj, message.arg1, message.arg2);
} else if (message.what == R.id.quit) {
    running = false;
    Looper.myLooper().quit();
}}
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();
  }
}
4

1 回答 1

0

ZXing 检测每一个接收到的帧,直到找到正确的信息。图像保存点是 ZXing 返回一个不为空的字符串时。此外,您可以使用不同的名称“时间戳+.jpg”保存文件,以防之前的文件被覆盖。

于 2013-08-14T14:00:05.863 回答