我正在创建一个我希望扫描qrcode的应用程序,但是当我尝试扫描qrcode时,我的应用程序崩溃,并显示错误,以下是我的代码段和logcat,任何人都可以告诉这段代码有什么问题以及为什么扫描工作不正常
MainMenu.java
rl_scan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainMenu.this, ScanDispatch.class);
startActivity(intent);
finish();
}
});
ScanDispatch.java
public class ScanDispatch extends Activity {
private TextView current_office_name;
// scanning
private Camera mCamera;
private CameraPreview mPreview;
private Handler autoFocusHandler;
ImageScanner scanner;
boolean barcodeScanned = false;
boolean previewing = true;
// private DrawerLayout mDrawerLayout;
ImageView img_navigation_link;
private FrameLayout preview;
public ScanDispatch() {
}
// @Override
// public View onCreateView(LayoutInflater inflater, ViewGroup container,
// Bundle savedInstanceState) {
//
// View rootView = inflater.inflate(R.layout.activity_scan_dispatch,
// container, false);
// current_office_name = (TextView)
// rootView.findViewById(R.id.current_office_name);
// current_office_name.setText(consts.pref.getString("office_name", ""));
// btn_scan = (ImageButton) rootView.findViewById(R.id.btn_scan);
//
// return rootView;
// }
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scan_dispatch);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
consts.pref = getSharedPreferences("pref", MODE_PRIVATE);
consts.editor = consts.pref.edit();
current_office_name = (TextView) findViewById(R.id.current_office_name);
current_office_name.setText(consts.pref.getString("office_name", ""));
// btn_scan = (ImageButton) findViewById(R.id.btn_scan);
img_navigation_link = (ImageView) findViewById(R.id.img_navigation_link);
// scanner
autoFocusHandler = new Handler();
mCamera = getCameraInstance();
scanner = new ImageScanner();
scanner.setConfig(0, Config.X_DENSITY, 3);
scanner.setConfig(0, Config.Y_DENSITY, 3);
mPreview = new CameraPreview(this, mCamera, previewCb, autoFocusCB);
try {
if (barcodeScanned) {
barcodeScanned = false;
// scanText.setText("Scanning...");
mCamera.setPreviewCallback(previewCb);
mCamera.startPreview();
previewing = true;
mCamera.autoFocus(autoFocusCB);
}
} catch (ActivityNotFoundException anfe) {
anfe.printStackTrace();
}
preview = (FrameLayout) findViewById(R.id.relativeLayout1);
preview.addView(mPreview);
img_navigation_link.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// mCamera.release();
//preview.removeView(mPreview);
/*mCamera.setPreviewCallback(null);
mCamera.release();
preview.removeView(mPreview);*/
Intent intent = new Intent(ScanDispatch.this, MainMenu.class);
startActivity(intent);
}
});
}
public void onPause() {
super.onPause();
releaseCamera();
}
public void onResume() {
super.onResume();
if (mCamera == null) {
mCamera = getCameraInstance();
}
if (mPreview == null) {
mCamera.setPreviewCallback(previewCb);
mCamera.startPreview();
previewing = true;
mCamera.autoFocus(autoFocusCB);
mPreview = new CameraPreview(getApplicationContext(), mCamera, previewCb, autoFocusCB);
preview.addView(mPreview);
}
}
@SuppressWarnings("deprecation")
public static Camera getCameraInstance() {
Camera c = null;
try {
c = Camera.open();
} catch (Exception e) {
}
return c;
}
static {
System.loadLibrary("iconv");
}
private Runnable doAutoFocus = new Runnable() {
@SuppressWarnings("deprecation")
public void run() {
if (previewing)
mCamera.autoFocus(autoFocusCB);
}
};
private void releaseCamera() {
if (mCamera != null) {
previewing = false;
mCamera.setPreviewCallback(null);
mCamera.release();
mCamera = null;
}
}
// Mimic continuous auto-focusing
@SuppressWarnings("deprecation")
AutoFocusCallback autoFocusCB = new AutoFocusCallback() {
public void onAutoFocus(boolean success, Camera camera) {
autoFocusHandler.postDelayed(doAutoFocus, 1000);
}
};
@SuppressWarnings("deprecation")
PreviewCallback previewCb = new PreviewCallback() {
private String degd;
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("barcode result " + sym.getData());
Log.d("ooo", "Barcode result:" + sym.getData());
String contents = sym.getData();
consts.editor.putString("qrcode", contents);
consts.editor.commit();
barcodeScanned = true;
Intent intent = new Intent(ScanDispatch.this, DispatchTracking.class);
intent.putExtra("arrow_val", "1");
startActivity(intent);
mCamera.release();
ScanDispatch.this.finish();
// Fragment fragment = new DispatchTracking();
//
// if (fragment != null) {
// FragmentManager fragmentManager = getFragmentManager();
// fragmentManager.beginTransaction()
// .replace(R.id.content_frame, fragment).commit();
// setTitle("Dispatch Me");
// mDrawerLayout.closeDrawer(mDrawerList);
//
// }
}
}
}
};
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
if (resultCode == Activity.RESULT_OK) {
String contents = intent.getStringExtra("SCAN_RESULT");
String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
consts.editor.putString("qrcode", contents);
consts.editor.commit();
// Toast.makeText(this, "Content:" + contents + " Format:" +
// format, Toast.LENGTH_LONG).show();
Intent intent2 = new Intent(ScanDispatch.this, DispatchTracking.class);
intent2.putExtra("arrow_val", "1");
startActivity(intent2);
ScanDispatch.this.finish();
// Fragment fragment = new DispatchTracking();
//
// if (fragment != null) {
// FragmentManager fragmentManager = getFragmentManager();
// fragmentManager.beginTransaction()
// .replace(R.id.content_frame, fragment).commit();
//
// // setTitle("Dispatch Me");
// // mDrawerLayout.closeDrawer(mDrawerList);
//
// }
}
}
}
CameraPreview.java
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera mCamera;
private PreviewCallback previewCallback;
private AutoFocusCallback autoFocusCallback;
public CameraPreview(Context context, Camera camera,
PreviewCallback previewCb,
AutoFocusCallback autoFocusCb) {
super(context);
mCamera = camera;
previewCallback = previewCb;
autoFocusCallback = autoFocusCb;
/*
* Set camera to continuous focus if supported, otherwise use
* software auto-focus. Only works for API level >=9.
*/
/*
Camera.Parameters parameters = camera.getParameters();
for (String f : parameters.getSupportedFocusModes()) {
if (f == Parameters.FOCUS_MODE_CONTINUOUS_PICTURE) {
mCamera.setFocusMode(Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
autoFocusCallback = null;
break;
}
}
*/
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
// deprecated setting, but required on Android versions prior to 3.0
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
@SuppressWarnings("deprecation")
public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, now tell the camera where to draw the preview.
try {
mCamera.setPreviewDisplay(holder);
} catch (IOException e) {
Log.d("DBG", "Error setting camera preview: " + e.getMessage());
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
// Camera preview released in activity
/*this.getHolder().removeCallback(this);
mCamera.stopPreview();
mCamera.release();*/
//mCamera.release();
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
/*
* If your preview can change or rotate, take care of those events here.
* Make sure to stop the preview before resizing or reformatting it.
*/
if (mHolder.getSurface() == null){
// preview surface does not exist
return;
}
// stop preview before making changes
try {
mCamera.stopPreview();
} catch (Exception e){
// ignore: tried to stop a non-existent preview
}
try {
// Hard code camera surface rotation 90 degs to match Activity view in portrait
mCamera.setDisplayOrientation(90);
mCamera.setPreviewDisplay(mHolder);
mCamera.setPreviewCallback(previewCallback);
mCamera.startPreview();
mCamera.autoFocus(autoFocusCallback);
} catch (Exception e){
Log.d("DBG", "Error starting camera preview: " + e.getMessage());
}
}
}
logcat的
05-23 10:54:05.760: E/AndroidRuntime(10791): FATAL EXCEPTION: main
05-23 10:54:05.760: E/AndroidRuntime(10791): java.lang.RuntimeException: Unable to pause activity {com.dispatchme/com.dispatchme.ScanDispatch}: java.lang.RuntimeException: Method called after release()
05-23 10:54:05.760: E/AndroidRuntime(10791): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2720)
05-23 10:54:05.760: E/AndroidRuntime(10791): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2676)
05-23 10:54:05.760: E/AndroidRuntime(10791): at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:2654)
-23 10:54:05.760: E/AndroidRuntime(10791): at android.app.ActivityThread.access$800(ActivityThread.java:128)
05-23 10:54:05.760: E/AndroidRuntime(10791): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1168)
05-23 10:54:05.760: E/AndroidRuntime(10791): at android.os.Handler.dispatchMessage(Handler.java:99)
05-23 10:54:05.760: E/AndroidRuntime(10791): at android.os.Looper.loop(Looper.java:137)
05-23 10:54:05.760: E/AndroidRuntime(10791): at android.app.ActivityThread.main(ActivityThread.java:4517)
05-23 10:54:05.760: E/AndroidRuntime(10791): at java.lang.reflect.Method.invokeNative(Native Method)
05-23 10:54:05.760: E/AndroidRuntime(10791): at java.lang.reflect.Method.invoke(Method.java:511)
05-23 10:54:05.760: E/AndroidRuntime(10791): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
05-23 10:54:05.760: E/AndroidRuntime(10791): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
05-23 10:54:05.760: E/AndroidRuntime(10791): at dalvik.system.NativeStart.main(Native Method)
05-23 10:54:05.760: E/AndroidRuntime(10791): Caused by: java.lang.RuntimeException: Method called after release()
05-23 10:54:05.760: E/AndroidRuntime(10791): at android.hardware.Camera.setHasPreviewCallback(Native Method)
05-23 10:54:05.760: E/AndroidRuntime(10791): at android.hardware.Camera.setPreviewCallback(Camera.java:590)
05-23 10:54:05.760: E/AndroidRuntime(10791): at com.dispatchme.ScanDispatch.releaseCamera(ScanDispatch.java:193)
05-23 10:54:05.760: E/AndroidRuntime(10791): at com.dispatchme.ScanDispatch.onPause(ScanDispatch.java:142)
05-23 10:54:05.760: E/AndroidRuntime(10791): at android.app.Activity.performPause(Activity.java:4568)
05-23 10:54:05.760: E/AndroidRuntime(10791): at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1199)
05-23 10:54:05.760: E/AndroidRuntime(10791): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2707)
05-23 10:54:05.760: E/AndroidRuntime(10791): ... 12 more
05-23 10:54:05.860: E/AlarmManagerService(407): android_server_AlarmManagerService_set to type=3, 5838.532000000
答案 0 :(得分:0)
我猜问题可能出在ScanDispatche.java的onPause()
方法中。你在super.onPause()之后调用了一些方法。它应该是:
public void onPause() {
releaseCamera();
super.onPause();
}
这是因为Android 第一个调用了它的方法,而且对你的方法来说为时已晚。
答案 1 :(得分:0)
试试这个
public void onPause() {
super.onPause();
mCamera.cancelAutoFocus();
mCamera.setPreviewCallback(null);
mCamera.stopPreview();
mCamera.release();
}
@Override
protected void onResume() {
super.onResume();
// Open the default i.e. the first rear facing camera.
if (mCamera != null) {
mCamera.release();
}
mCamera = Camera.open();
if (mCamera == null) {
// Cancel request if mCamera is null.
cancelRequest();
return;
}
Camera.Parameters parameters = mCamera.getParameters();
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
mCamera.setParameters(parameters);
mPreview.setCamera(mCamera);
}