ANDROID ZXING:在onPreviewFrame中保存照片每帧都会保存一张照片。 如何在扫描时保存单张照片?

在过去的几个星期里,我一直试图改变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和function为中心,但可以通过意图使用Zxing(Zxing内置于应用程序构建路径中,是的,如果已经安装了Zxing,它可能会受到干扰)。 一旦Zxing扫描了QR码,其上编码的信息将返回到我的应用程序并存储,然后在短暂的延迟后,Zxing会自动重新启动。

我的当前代码在Zxing运行时每帧保存一个图像,我想要的function是只保存扫描帧。 虽然Zxing停止在我的应用程序重新接管的短窗口中保存图像,但是Zxing很快被重新初始化,但我可能没有时间操纵数据。 但是,可能的解决方法是快速重命名已保存的文件,以便Zxing不会开始覆盖它,并且可以在后台执行操作。 然而,每帧保存图像是浪费资源而不是优选的。

如何在扫描时保存图像?

提前致谢。


已更新,以按要求显示找到的multiFormatReader实例:

 private final CaptureActivity activity; private final MultiFormatReader multiFormatReader; private boolean running = true; DecodeHandler(CaptureActivity activity, Map 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(); } } 

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