Android相机预览会突然冻结

时间:2015-09-16 10:11:24

标签: java android camera

我在官方developer.android.com教程的帮助下实现了一个Android摄像头。该应用程序有时工作正常,但大约3/5倍的相机预览在一些旋转和点击按钮后冻结甚至没有这些工作(其他元素不冻结)。最可爱的部分是,当我调试应用程序时,预览不会卡住,但是当我想要正常运行应用程序时,问题就会发生。

这是我的fullScreen类,它包含android studio为fullScreen活动生成的代码以及实现surfaceHolder.Callback和相机内容的代码。

public class CameraActivity extends Activity implements SurfaceHolder.Callback {

...   // some constants here
    private Camera mCamera;
    private SurfaceHolder mHolder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_camera);

        SurfaceView cameraSufaceView = (SurfaceView) findViewById(R.id.camera_preview);

        // Accessing front camera to take picture
        mCamera = openFrontFacingCameraGingerbread();

        if (mCamera != null) {
            // Install a SurfaceHolder.Callback so we get notified when the
            // underlying surface is created and destroyed.
            mHolder = cameraSufaceView.getHolder();
            mHolder.addCallback(this);

        } else {
            // Alter user
        }

    /**
     * Gets an instance of front facing camera if available
     */
    @SuppressWarnings("deprecation")
    private Camera openFrontFacingCameraGingerbread() {
        int cameraCount;
        Camera cam = null;
        Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
        cameraCount = Camera.getNumberOfCameras();
        for (int camIdx = 0; camIdx < cameraCount; camIdx++) {
            Camera.getCameraInfo(camIdx, cameraInfo);
            if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
                try {
                    cam = Camera.open(camIdx);
                } catch (RuntimeException e) {
                    Log.e(TAG, "Camera failed to open: " + e.getLocalizedMessage());
                }
            }
        }

        return cam;
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        // The Surface has been created, now tell the camera where to draw the preview.
        try {
            mCamera.setPreviewDisplay(holder);
            mCamera.startPreview();
        } catch (IOException e) {
            Log.d(TAG, "Error setting camera preview: " + e.getMessage());
        }

    }

    @Override
    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
        }

        // start preview with new settings
        try {
//            mCamera.setDisplayOrientation(needs degree here);
            mCamera.setPreviewDisplay(mHolder);
            mCamera.startPreview();

        } catch (Exception e) {
            Log.d(TAG, "Error starting camera preview: " + e.getMessage());
        }
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        releaseCamera();
    }

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

    @Override
    protected void onResume() {
        super.onResume();
        if (mCamera == null)
            mCamera = openFrontFacingCameraGingerbread();
    }

    private void releaseCamera() {
        if (mCamera != null) {
            mCamera.stopPreview();
            mCamera.release();        // release the camera for other applications
            mCamera = null;
        }
    }

当出现此问题时,LogCat中会出现一些错误,但它们并不能提供真正的信息。他们是:

26893-26893/com.naviiid.retinaflash E/art﹕ No implementation found for void java.lang.Runtime.appStartupEnd() (tried Java_java_lang_Runtime_appStartupEnd and Java_java_lang_Runtime_appStartupEnd__)
09-16 01:34:09.336  26893-26893/com.naviiid.retinaflash E/ActivityThread﹕ appStartupEnd :
    java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java)
            at android.app.ActivityThread.appStartupEnd(ActivityThread.java:305)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2819)
            at android.app.ActivityThread.access$900(ActivityThread.java:177)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:5942)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
     Caused by: java.lang.UnsatisfiedLinkError: No implementation found for void java.lang.Runtime.appStartupEnd() (tried Java_java_lang_Runtime_appStartupEnd and Java_java_lang_Runtime_appStartupEnd__)
            at java.lang.Runtime.appStartupEnd(Native Method)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java)
            at android.app.ActivityThread.appStartupEnd(ActivityThread.java:305)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2819)
            at android.app.ActivityThread.access$900(ActivityThread.java:177)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:5942)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)

到目前为止我发现的唯一线索是,即使活动没有暂停,有时android会调用onPause方法。为了再次获取相机的实例,我在onResume方法中调用openFrontFacingCameraGingerbread()

0 个答案:

没有答案