I have an application uploaded to Google Play. The app is supposed to scan a QR-code from a pole to register a visit. It works on all devices but Sony XPERIA models. In Google Play Developer Console I get a lot of one particular crash:

java.lang.RuntimeException: autoFocus failed
at android.hardware.Camera.native_autoFocus(Native Method)
at android.hardware.Camera.autoFocus(Camera.java:975)
at me.dm7.barcodescanner.core.CameraPreview$1.run(CameraPreview.java:196)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:213)
at android.app.ActivityThread.main(ActivityThread.java:5225)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
at dalvik.system.NativeStart.main(Native Method) 

The only place I call the camera is is here:

public class ScannerActivity extends Activity implements ZXingScannerView.ResultHandler {

private ZXingScannerView m_ScannerView;


@Override
public void onCreate(Bundle state) {

  super.onCreate(state);
  m_ScannerView = new ZXingScannerView(this);
  setContentView(m_ScannerView);
}

@Override
public void onResume() {

  super.onResume();
  m_ScannerView.setResultHandler(ScannerActivity.this);
  m_ScannerView.startCamera();
}

@Override
public void onPause() {

  super.onPause();
  m_ScannerView.stopCamera();
}
有帮助吗?

解决方案

As far as I can tell, this is a bug in ZXing. You can implement a workaround by replacing ZXingSurfaceView:AutoFocus with an implementation that catches the exception. (You'll also have to replace a few other files if you go this route, or re-compile ZXing on your own). This doesn't resolve the root cause, though.

This bug was fixed in ZXing on July 29 2015, so updating to the latest version is probably easier.

public void AutoFocus()
{
    if (camera != null)
    {
        if (!tokenSource.IsCancellationRequested)
        {
            global::Android.Util.Log.Debug("ZXING", "AutoFocus Requested");

            try
            {
                camera.AutoFocus(this);
            }
               catch (RuntimeException ex)
            {
                Console.WriteLine("ZXING: Warning: Caught RuntimeException during AutoFocus."); 
            }
        }
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top