ANDROID ZXING:保存在previewFrame照片保存的照片每一帧。如何既节省扫描后一张照片? [英] ANDROID ZXING: Saving a photo in onPreviewFrame saves a photo every frame. How to only save a single photo upon scan?

查看:440
本文介绍了ANDROID ZXING:保存在previewFrame照片保存的照片每一帧。如何既节省扫描后一张照片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在过去的几周里,我一直在试图改变Zxing到扫描后立即拍照。感谢帮助我在那里我可以一直从上previewFrame类节省previewCallback.java

内的图像点

在code我上previewMethod方法中使用,应当遵循,再怎么我的应用程序工作的简短纲要。

 上previewFrame公共无效(字节[]数据,相机摄像头){
点cameraResolution = configManager.getCameraResolution();
处理程序中的previewHandler = previewHandler;android.hardware.Camera.Parameters参数= camera.getParameters();
android.hardware.Camera.Size大小= parameters.get previewSize();INT高度= size.height;
INT宽度= size.width;的System.out.println(高度+高);
的System.out.println(宽度+宽);如果(cameraResolution = NULL&放大器;!&放大器;!在previewHandler = NULL){
    YuvImage IM =新YuvImage(数据,ImageFormat.NV21,宽度,
            高度,NULL);
            矩形R =新的Rect(0,0,宽度,高度);
            ByteArrayOutputStream BAOS =新ByteArrayOutputStream();
            im.com pressToJpeg(R,50,BAOS);            尝试{
                FileOutputStream中输出=新的FileOutputStream(/ SD卡/ test_jpg.jpg);
                output.write(baos.toByteArray());
                output.flush();
                output.close();                的System.out.println(试图保存文件);
                的System.out.println(数据);            }赶上(FileNotFoundException异常五){                的System.out.println(保存到文件失败);            }赶上(IOException异常五){                的System.out.println(保存到文件失败);            }
    消息消息=的previewHandler.obtainMessage(previewMessage,cameraResolution.x,
      cameraResolution.y,数据);
  message.sendToTarget();
  previewHandler = NULL;}其他{
  Log.d(TAG,得到了preVIEW回调,但没有处理程序或可用分辨率);
}}

围绕它自己的GUI和功能

我的应用中心,但可以通过意向从事Zxing(Zxing内置到应用程序构建路径,是的,这是坏的,如果已经安装Zxing它可以intefere)。一旦Zxing已扫描的QR code,信息EN codeD上它返回到我的应用程序和存储,然后在短暂的延迟后Zxing会自动重新启动。

我目前的code节省每一帧而Zxing正在运行的形象,我想的功能是只对扫描帧保存。虽然Zxing停止保存在短期窗口的图像在我的应用程序再次接管,Zxing很快但是重新初始化,我可能没有时间来处理数据。可能的解决方法不过很快重新命名保存的文件,以便Zxing不启动覆盖它和操作可以在后台进行。不过,保存图像的每一帧是对资源的浪费和不足preferable。

如何仅在扫描保存图像?

先谢谢了。


更新以显示multiFormatReader发现实例的要求:

 私人最终CaptureActivity活动;
私人最终MultiFormatReader multiFormatReader;
私人布尔运行= TRUE;
德$ C $钱德勒(CaptureActivity活动,地图<德codeHintType,对象>提示){
multiFormatReader =新MultiFormatReader();
multiFormatReader.setHints(提示);
this.activity =活动;
}
@覆盖
公共无效的handleMessage(消息消息){
如果(!运​​行){
  返回;
}
如果(message.what == R.id.de code){
    德code((字节[])message.obj,message.arg1,message.arg2);
}否则如果(message.what == R.id.quit){
    运行= FALSE;
    Looper.myLooper()退出()。
}}
私人无效德code(字节[]的数据,诠释的宽度,高度INT){
长启动= System.currentTimeMillis的();
结果rawResult = NULL;
PlanarYUVLuminanceSource源= activity.getCameraManager()buildLuminanceSource(数据,宽,高)。
如果(来源!= NULL){
  BinaryBitmap位=新BinaryBitmap(新HybridBinarizer(源));  //这里?  尝试{
    rawResult = multiFormatReader.de codeWithState(位图);
  }赶上(ReaderException重){
    //继续
  } {最后
    multiFormatReader.reset();
  }
}


解决方案

ZXing检测每一个接收到的帧,直到找出正确的信息。图像保存的一点是,当ZXing返回一个字符串,它不为空。此外,你可以保存不同的名称时间戳+ .JPG的文件,以防previous文件将被覆盖。

For the last few weeks I have been attempting to alter Zxing to take a photo immediately upon scan. Thanks to help I am at a point where I can be consistently saving an image from the onPreviewFrame class within PreviewCallback.java

The code I use within the onPreviewMethod method shall follow, and then a short rundown of how my app works.

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

My application centers around its own GUI and functionality, but can engage Zxing via intent (Zxing is built into the apps build path, yes this is bad as it can intefere if Zxing is already installed). Once Zxing has scanned a QR code, the information encoded on it is returned to my app and stored, and then after a short delay Zxing is automatically re-initiated.

My current code saves an image every frame whilst Zxing is running, the functionality I would like is to have only the frame on scan be saved. Although Zxing stops saving images in the short window where my app takes over again, Zxing is quickly re-initialized however and I may not have time to manipulate the data. A possible workaround however is quickly renaming the saved file so that Zxing doesn't start overwriting it and manipulation can be performed in the background. Nevertheless, saving an image every frame is a waste of resources and less than preferable.

How do I only save an image upon scan?

Thanks in advance.


Updated to show found instances of multiFormatReader as requested:

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

解决方案

ZXing detects every received frame until finds out correct information. The image saving point is when ZXing returns a string which is not null. In addition, you can save file with different name "timestamp + .jpg", in case previous file will be overwritten.

这篇关于ANDROID ZXING:保存在previewFrame照片保存的照片每一帧。如何既节省扫描后一张照片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆