gpt4 book ai didi

android - Zbar 与 Android : Scanner camera viewport remain inactive and black after showing the url in browser

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:15:41 24 4
gpt4 key购买 nike

我的项目中需要一个二维码扫描器。我使用 ZBar 来完成这项任务。主要是我对 ZBar git 示例给出的示例代码做了非常小的更改来完成这项工作。我需要在浏览器(如果是 url)或对话框(如果是正常信息)中显示扫描结果。

除了我在浏览器中扫描二维码后尝试显示 url 之外,下面给出的代码都可以很好地工作。当我从浏览器返回我的应用程序时,相机视口(viewport)变黑并保持不活动状态。我试图让相机恢复 Activity 状态以扫描另一个二维码,但我失败了。我试图在我的 onResume() 函数中重新打开相机,但这会导致错误并强制退出应用程序。请帮我解决这个问题。

    package com.myapp;
import net.sourceforge.zbar.Config;
import net.sourceforge.zbar.Image;
import net.sourceforge.zbar.ImageScanner;
import net.sourceforge.zbar.Symbol;
import net.sourceforge.zbar.SymbolSet;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.hardware.Camera;
import android.hardware.Camera.AutoFocusCallback;
import android.hardware.Camera.PreviewCallback;
import android.hardware.Camera.Size;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.webkit.URLUtil;
import android.widget.FrameLayout;

import com.myapp.CameraPreview;
/* Import ZBar Class files */

public class QRScannerActivity extends Activity
{
private Camera mCamera;
private CameraPreview mPreview;
private Handler autoFocusHandler;

ImageScanner scanner;

private boolean barcodeScanned = false;
private boolean previewing = true;

static {
System.loadLibrary("iconv");
}

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.qr_scanner_main);

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

autoFocusHandler = new Handler();
mCamera = getCameraInstance();

/* Instance barcode scanner */
scanner = new ImageScanner();
scanner.setConfig(0, Config.X_DENSITY, 3);
scanner.setConfig(0, Config.Y_DENSITY, 3);

mPreview = new CameraPreview(this, mCamera, previewCb, autoFocusCB);
FrameLayout preview = (FrameLayout)findViewById(R.id.cameraPreview);
preview.addView(mPreview);
}

@Override
protected void onPause() {
super.onPause();
releaseCamera();
//finish();
}

/** A safe way to get an instance of the Camera object. */
public static Camera getCameraInstance(){
Camera c = null;
try {
c = Camera.open();
} catch (Exception e){
}
return c;
}

private void releaseCamera() {
//Toast.makeText(QRScannerActivity.this, "Paused State", Toast.LENGTH_SHORT).show();
if (mCamera != null) {
previewing = false;
mCamera.setPreviewCallback(null);
mPreview.getHolder().removeCallback(mPreview);
mCamera.release();
mCamera = null;
}

}

private Runnable doAutoFocus = new Runnable() {
public void run() {
if (previewing)
mCamera.autoFocus(autoFocusCB);
}
};

PreviewCallback previewCb = new PreviewCallback() {
public void onPreviewFrame(byte[] data, Camera camera) {
Camera.Parameters parameters = camera.getParameters();
Size size = parameters.getPreviewSize();

Image barcode = new Image(size.width, size.height, "Y800");
barcode.setData(data);

int result = scanner.scanImage(barcode);
String QRScannerResult;

if (result != 0) {
previewing = false;
mCamera.setPreviewCallback(null);
mCamera.stopPreview();

SymbolSet syms = scanner.getResults();
for (Symbol sym : syms) {
QRScannerResult = sym.getData();
showResultAction(QRScannerResult);
barcodeScanned = true;

}
}
}
};

// Mimic continuous auto-focusing
AutoFocusCallback autoFocusCB = new AutoFocusCallback() {
public void onAutoFocus(boolean success, Camera camera) {
autoFocusHandler.postDelayed(doAutoFocus, 1000);
}
};

private void showResultAction(String QRScannerResult){
if (URLUtil.isValidUrl(QRScannerResult)) {

if (barcodeScanned) {
barcodeScanned = false;
mCamera.setPreviewCallback(previewCb);
mCamera.startPreview();
previewing = true;
mCamera.autoFocus(autoFocusCB);
}

Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(QRScannerResult));
startActivity(i);

}else {
new AlertDialog.Builder(this)
.setTitle("QR Data")
.setMessage(QRScannerResult)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (barcodeScanned) {
barcodeScanned = false;
mCamera.setPreviewCallback(previewCb);
mCamera.startPreview();
previewing = true;
mCamera.autoFocus(autoFocusCB);
}
}
})
.show();
}

}
}

提前致谢。萨基布

最佳答案

最后我成功地找出了我的代码的问题并得到了解决方案。当我尝试在我的 onResume() 方法中重新打开相机时,我错过了一部分。当我在 onResume() 中创建/打开一个新相机时,FrameLayout 仍然有我以前的相机。所以我所做的就是在 onPause() 方法上从 FrameLayout 中删除我以前的相机,然后在 onResume() 方法上重新创建所有内容。这解决了我的问题,现在它运行良好,没有任何错误。这是我的 onPause()onResume() 方法以及修复方法。希望这对将来的人有所帮助。

    public void onPause() {
super.onPause();
releaseCamera();
FrameLayout preview = (FrameLayout)findViewById(R.id.cameraPreview);
preview.removeView(mPreview);
}

public void onResume(){
super.onResume();

try {
if(mCamera==null){

//setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
autoFocusHandler = new Handler();
mCamera = getCameraInstance();
this.getWindowManager().getDefaultDisplay().getRotation();

scanner = new ImageScanner();
scanner.setConfig(0, Config.X_DENSITY, 3);
scanner.setConfig(0, Config.Y_DENSITY, 3);

mPreview = new CameraPreview(this, mCamera, previewCb, autoFocusCB);
FrameLayout preview = (FrameLayout)findViewById(R.id.cameraPreview);
preview.addView(mPreview);
}
} catch (Exception e) {
// TODO Auto-generated catch block

}
}

/** A safe way to get an instance of the Camera object. */
public static Camera getCameraInstance(){
Camera c = null;
try {
c = Camera.open();
} catch (Exception e){
}
return c;
}

private void releaseCamera() {
//Toast.makeText(QRScannerActivity.this, "Paused State", Toast.LENGTH_SHORT).show();
if (mCamera != null) {
previewing = false;
mCamera.setPreviewCallback(null);
mPreview.getHolder().removeCallback(mPreview);
mCamera.release();
mCamera = null;
mPreview= null;
}

}

谢谢,萨基布

关于android - Zbar 与 Android : Scanner camera viewport remain inactive and black after showing the url in browser,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17826314/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com