【问题标题】:ANDROID ZXING: Saving a photo in onPreviewFrame saves a photo every frame. How to only save a single photo upon scan?ANDROID ZXING:在 onPreviewFrame 中保存照片每帧都会保存一张照片。如何在扫描时只保存一张照片?
【发布时间】:2013-08-16 11:31:07
【问题描述】:

在过去的几周里,我一直在尝试改变 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 扫描 QR 码后,其上编码的信息将返回到我的应用程序并存储,然后在短暂延迟后自动重新启动 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();
  }
}

【问题讨论】:

    标签: java android save photo zxing


    【解决方案1】:

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

    【讨论】:

    • 我不明白字符串返回点在哪里。不过,我确实可以通过在我自己的应用程序中重命名最近保存的图像来使代码正常工作。
    • 这是我的代码: String result = new MultiFormatReader().decode(bitmap, hints);尝试搜索类似的东西。在ZXing,看看Class MultiFormatReader
    • 我能找到的唯一 MultiFormatReader 实例是已经讨论过的实例。 OP 已更新以在 DecodeHandler.java 中显示此代码。从这里做什么是我无法做到的,我感谢你一直以来的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-18
    • 2013-05-31
    相关资源
    最近更新 更多