带有 ZBar 库的 QR 码阅读器在 Intent ActivityNotFound 上出现错误并崩溃。我更改为 Intent.ACTION_WEB_SEARCH 但没有结果。所有的二维码都没有显示错误,这就是为什么我不明白出了什么问题。
public class MainActivity extends Activity implements OnClickListener {
private boolean isLighOn = false;
private Camera mCamera;
private CameraPreview mPreview;
private Handler autoFocusHandler;
TextView scanText;
Button scanButton, flash;
String bar;
ImageScanner scanner;
private boolean previewing = true;
static {
System.loadLibrary("iconv");
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.activity_main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
autoFocusHandler = new Handler();
mCamera = getCameraInstance();
flash=(Button)findViewById(R.id.flash);
flash.setOnClickListener(this);
/* 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);
scanText = (TextView) findViewById(R.id.scanText);
}
public void onPause() {
super.onPause();
// releaseCamera();
}
/** 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() { if (mCamera != null) { previewing = false;
* mCamera.setPreviewCallback(null); 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);
if (result != 0) {
previewing = false;
mCamera.setPreviewCallback(null);
mCamera.stopPreview();
SymbolSet syms = scanner.getResults();
for (Symbol sym : syms) {
scanText.setText(sym.getData());
bar = scanText.getText().toString();
try {
Intent inte = new Intent(Intent.ACTION_VIEW, Uri.parse(bar));
startActivity(inte);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
}
scanText.setVisibility(View.INVISIBLE);
}
}
}
};
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
getCameraInstance();
scanText.setText("Scaning...");
scanText.setVisibility(View.VISIBLE);
}
// Mimic continuous auto-focusing
AutoFocusCallback autoFocusCB = new AutoFocusCallback() {
public void onAutoFocus(boolean success, Camera camera) {
autoFocusHandler.postDelayed(doAutoFocus, 1000);
}
};
@Override
public void startActivityForResult(Intent intent, int requestCode) {
// TODO Auto-generated method stub
super.startActivityForResult(intent, requestCode);
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Parameters p = mCamera.getParameters();
/*p.setFlashMode(Parameters.FLASH_MODE_TORCH);
mCamera.setParameters(p);
p.setFlashMode(Parameters.FLASH_MODE_OFF);
mCamera.setParameters(p);*/
if (isLighOn) {
Log.i("info", "torch is turn off!");
p.setFlashMode(Parameters.FLASH_MODE_OFF);
mCamera.setParameters(p);
isLighOn = false;
} else {
Log.i("info", "torch is turn on!");
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
mCamera.setParameters(p);
isLighOn = true;
}
}
}
和 LogCat
11-12 12:09:05.484: W/System.err(4746): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=5949033011551 }
11-12 12:09:05.484: W/System.err(4746): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1580)
11-12 12:09:05.489: W/System.err(4746): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1431)
11-12 12:09:05.489: W/System.err(4746): at android.app.Activity.startActivityForResult(Activity.java:3446)
11-12 12:09:05.489: W/System.err(4746): at android.app.Activity.startActivityForResult(Activity.java:3407)
11-12 12:09:05.489: W/System.err(4746): at com.example.qrcodereader.MainActivity.startActivityForResult(MainActivity.java:148)
11-12 12:09:05.489: W/System.err(4746): at android.app.Activity.startActivity(Activity.java:3617)
11-12 12:09:05.489: W/System.err(4746): at android.app.Activity.startActivity(Activity.java:3585)
11-12 12:09:05.494: W/System.err(4746): at com.example.qrcodereader.MainActivity$2.onPreviewFrame(MainActivity.java:116)
11-12 12:09:05.499: W/System.err(4746): at android.hardware.Camera$EventHandler.handleMessage(Camera.java:841)
11-12 12:09:05.499: W/System.err(4746): at android.os.Handler.dispatchMessage(Handler.java:99)
11-12 12:09:05.499: W/System.err(4746): at android.os.Looper.loop(Looper.java:137)
11-12 12:09:05.504: W/System.err(4746): at android.app.ActivityThread.main(ActivityThread.java:4921)
11-12 12:09:05.504: W/System.err(4746): at java.lang.reflect.Method.invokeNative(Native Method)
11-12 12:09:05.504: W/System.err(4746): at java.lang.reflect.Method.invoke(Method.java:511)
11-12 12:09:05.509: W/System.err(4746): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
11-12 12:09:05.509: W/System.err(4746): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
11-12 12:09:05.509: W/System.err(4746): at dalvik.system.NativeStart.main(Native Method)