android - 立即拍摄2张照片-使应用程序崩溃

标签 android crash camera surfaceview image

当我一个接一个快速拍摄两张照片时,应用程序崩溃。如何防止车祸?
注意:我可以拍摄几张照片,但是两次拍摄之间的时间间隔大约为500毫秒。
这个

这是logcat:

07-07 23:51:31.676: E/AndroidRuntime(10799): FATAL EXCEPTION: main
07-07 23:51:31.676: E/AndroidRuntime(10799): java.lang.IllegalStateException: Could not execute method of the activity
07-07 23:51:31.676: E/AndroidRuntime(10799):    at android.view.View$1.onClick(View.java:3063)
07-07 23:51:31.676: E/AndroidRuntime(10799):    at android.view.View.performClick(View.java:3534)
07-07 23:51:31.676: E/AndroidRuntime(10799):    at android.view.View$PerformClick.run(View.java:14263)
07-07 23:51:31.676: E/AndroidRuntime(10799):    at android.os.Handler.handleCallback(Handler.java:605)
07-07 23:51:31.676: E/AndroidRuntime(10799):    at android.os.Handler.dispatchMessage(Handler.java:92)
07-07 23:51:31.676: E/AndroidRuntime(10799):    at android.os.Looper.loop(Looper.java:137)
07-07 23:51:31.676: E/AndroidRuntime(10799):    at android.app.ActivityThread.main(ActivityThread.java:4441)
07-07 23:51:31.676: E/AndroidRuntime(10799):    at java.lang.reflect.Method.invokeNative(Native Method)
07-07 23:51:31.676: E/AndroidRuntime(10799):    at java.lang.reflect.Method.invoke(Method.java:511)
07-07 23:51:31.676: E/AndroidRuntime(10799):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-07 23:51:31.676: E/AndroidRuntime(10799):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-07 23:51:31.676: E/AndroidRuntime(10799):    at dalvik.system.NativeStart.main(Native Method)
07-07 23:51:31.676: E/AndroidRuntime(10799): Caused by: java.lang.reflect.InvocationTargetException
07-07 23:51:31.676: E/AndroidRuntime(10799):    at java.lang.reflect.Method.invokeNative(Native Method)
07-07 23:51:31.676: E/AndroidRuntime(10799):    at java.lang.reflect.Method.invoke(Method.java:511)
07-07 23:51:31.676: E/AndroidRuntime(10799):    at android.view.View$1.onClick(View.java:3058)
07-07 23:51:31.676: E/AndroidRuntime(10799):    ... 11 more
07-07 23:51:31.676: E/AndroidRuntime(10799): Caused by: java.lang.RuntimeException: takePicture failed
07-07 23:51:31.676: E/AndroidRuntime(10799):    at android.hardware.Camera.native_takePicture(Native Method)
07-07 23:51:31.676: E/AndroidRuntime(10799):    at android.hardware.Camera.takePicture(Camera.java:948)
07-07 23:51:31.676: E/AndroidRuntime(10799):    at android.hardware.Camera.takePicture(Camera.java:893)
07-07 23:51:31.676: E/AndroidRuntime(10799):    at com.inturnex.safecam.MainActivity.TakePicture(MainActivity.java:134)
07-07 23:51:31.676: E/AndroidRuntime(10799):    ... 14 more

这是代码:
}
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_image_pick);
    m_surfaceView = (SurfaceView)findViewById(R.id.preview);
    m_surfaceView.getHolder().addCallback(this);
    m_camera = Camera.open();

    m_numOfCamera = Camera.getNumberOfCameras();

    CameraInfo cameraInfo = new CameraInfo();
    for (int i = 0; i < m_numOfCamera; ++i) {
        Camera.getCameraInfo(i, cameraInfo);
        if (cameraInfo.facing == CameraInfo.CAMERA_FACING_BACK) {
            m_defaultCameraId = i;
            m_currentCamera = m_defaultCameraId;
        }       
    }
    photoHandler = new PhotoHandler(this, dbHelper);
    }
}

@Override
public void onPause() {
    super.onPause();
    if(m_camera != null){
    m_camera.stopPreview();
    }
}
@Override
public void onResume() {
    super.onResume();
    if(m_camera != null){
    m_camera.startPreview();
    }
}
@Override
public void onDestroy() {
    super.onDestroy();
    if(m_camera != null){
    m_camera.release();
    }
}


public void TakePicture(View v) throws InterruptedException
{
    Log.d(Global.TAG,"In TakePicture");
    m_camera.takePicture(null, null, photoHandler);
}
@Override
public void surfaceChanged(SurfaceHolder arg0, int format, int w, int h) {
    m_surfaceWidth = w;
    m_surfaceHeight = h;
    Camera.Parameters params = m_camera.getParameters();
    List<Camera.Size> sizes = params.getSupportedPreviewSizes();
    Camera.Size selected = getOptimalPreviewSize(sizes, w, h);

    params.setPreviewSize(selected.width,  selected.height);


    m_camera.setParameters(params);
    setCameraDisplayOrientation(this, m_currentCamera, m_camera);

    m_camera.startPreview();    
}
static int degrees = 0;
private static void setCameraDisplayOrientation(Activity activity,
         int cameraId, android.hardware.Camera camera) {
     android.hardware.Camera.CameraInfo info =
             new android.hardware.Camera.CameraInfo();
     android.hardware.Camera.getCameraInfo(cameraId, info);
     int rotation = activity.getWindowManager().getDefaultDisplay()
             .getRotation();
     degrees = 0;
     switch (rotation) {
         case Surface.ROTATION_0: degrees = 0; break;
         case Surface.ROTATION_90: degrees = 90; break;
         case Surface.ROTATION_180: degrees = 180; break;
         case Surface.ROTATION_270: degrees = 270; break;
     }

     int result;
     if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
         result = (info.orientation + degrees) % 360;
         result = (360 - result) % 360;  // compensate the mirror
     } else {  // back-facing
         result = (info.orientation - degrees + 360) % 360;
     }
     camera.setDisplayOrientation(result);
 }


public void surfaceCreated(SurfaceHolder arg0) {
    try {
        m_camera.setPreviewDisplay(m_surfaceView.getHolder());
    } catch (Exception e) {
        e.printStackTrace();
    }   
}

public void surfaceDestroyed(SurfaceHolder arg0) {
    // TODO Auto-generated method stub

}

private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) {


    final double ASPECT_TOLERANCE = 0.1;
    double targetRatio = (double) w / h;
    if (sizes == null)
        return null;

    Size optimalSize = null;
    double minDiff = Double.MAX_VALUE;

    int targetHeight = h;

    // Try to find an size match aspect ratio and size
    for (Size size : sizes) {
        double ratio = (double) size.width / size.height;
        if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE)
            continue;
        if (Math.abs(size.height - targetHeight) < minDiff) {
            optimalSize = size;
            minDiff = Math.abs(size.height - targetHeight);
        }
    }

    // Cannot find the one match the aspect ratio, ignore the requirement
    if (optimalSize == null) {
        minDiff = Double.MAX_VALUE;
        for (Size size : sizes) {
            if (Math.abs(size.height - targetHeight) < minDiff) {
                optimalSize = size;
                minDiff = Math.abs(size.height - targetHeight);
            }
        }
    }

    return optimalSize;
}

谢谢!

最佳答案

可能不值得,但我要指出,从理论上讲,不可能以这种方式使用硬件。
当您尝试拍摄第二张照片时,仍在使用硬件拍摄第一张照片,因此,程序不知道要去哪里,因此崩溃了。

只是以为我会指出这个因素,尽管我可能对此不满意。

关于android - 立即拍摄2张照片-使应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17516328/

相关文章:

android - 如何从原生相机获取特定尺寸的图像

android - 如何正确地扩大内容的大小并添加到 ViewPager?

Android 为什么从 BackStack 返回后 switchCompat.setChecked(true) 不工作

android - 如何在Android xml中制作两条平行且相邻的线?

android - 如果应用程序崩溃或被手动终止,如何清除数据?

ios - 快门动画 AVFoundation iphone

android - 无法启动应用程序

c - 关于无限循环和内存崩溃

ios - 启动屏幕后,iOS App崩溃。对崩溃日志有帮助吗?

camera - Three.js - 如何计算两个 3D 位置之间的距离?