2

我的应用程序具有相机功能。它在所有 Android 版本中都可以正常工作,但现在当我在 S3 中测试时它崩溃了。错误指向这一行:

Parameters parameters = mCamera.getParameters();

在:

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
    // Log.e(TAG, "surfaceChanged");
    // XXX stopPreview() will crash if preview is not running
    if (mPreviewRunning) {
        mCamera.stopPreview();
    }

    /**
     * CODE TO RESOLVE CAMERA ORIENTATION PROBLEM START
     */

    Display display = ((WindowManager) getSystemService(WINDOW_SERVICE))
            .getDefaultDisplay();
    int flashStatus = session1.getFlashStatus();
    Parameters parameters = mCamera.getParameters();
    if (flashStatus == 0) {
        btnFlash.setBackgroundResource(R.drawable.flash);
        parameters.setFlashMode(Parameters.FLASH_MODE_AUTO);
        mCamera.setParameters(parameters);
    } else if (flashStatus == 1) {
        btnFlash.setBackgroundResource(R.drawable.flash_on);
        parameters.setFlashMode(Parameters.FLASH_MODE_ON);
        mCamera.setParameters(parameters);
    } else {
        btnFlash.setBackgroundResource(R.drawable.flash_off);
        parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
        mCamera.setParameters(parameters);
    }
    int currentapiVersion = android.os.Build.VERSION.SDK_INT;

    if (currentapiVersion >= android.os.Build.VERSION_CODES.FROYO) {
        // Do something for froyo and above versions
        if (display.getRotation() == Surface.ROTATION_0) {
            // parameters.setPreviewSize(h, w);
            mCamera.setDisplayOrientation(90);
        }
        if (display.getRotation() == Surface.ROTATION_90) {
            // parameters.setPreviewSize(w, h);
        }
        if (display.getRotation() == Surface.ROTATION_180) {
            // parameters.setPreviewSize(h, w);
            mCamera.setDisplayOrientation(-90);
        }
        if (display.getRotation() == Surface.ROTATION_270) {
            // parameters.setPreviewSize(w, h);
            mCamera.setDisplayOrientation(90);
        }
    } else {
        // do something for phones running an SDK before level 9
        if (display.getOrientation() == Surface.ROTATION_0) {
            // parameters.setPreviewSize(h, w);
            mCamera.setDisplayOrientation(90);
        }
        if (display.getOrientation() == Surface.ROTATION_90) {
            mCamera.setDisplayOrientation(90);
            // parameters.setPreviewSize(w, h);
        }
        if (display.getOrientation() == Surface.ROTATION_180) {
            mCamera.setDisplayOrientation(90);
            // parameters.setPreviewSize(h, w);
        }
        if (display.getOrientation() == Surface.ROTATION_270) {
            mCamera.setDisplayOrientation(90);
            // parameters.setPreviewSize(w, h);
            // mCamera.setDisplayOrientation(180);
        }
    }

    mCamera.setParameters(parameters);
    /**
     * CODE TO RESOLVE CAMERA ORIENTATION PROBLEM END
     */
    try {
        mCamera.setPreviewDisplay(mSurfaceHolder);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    if (mPreviewRunning) {
        mCamera.stopPreview();
        mCamera.startPreview();
        mPreviewRunning = true;
    } else {
        mCamera.startPreview();
        mPreviewRunning = true;
    }

}

public void surfaceDestroyed(SurfaceHolder holder) {
    Log.e(TAG, "surfaceDestroyed");
    if (mPreviewRunning) {
        mCamera.stopPreview();
        mPreviewRunning = false;
        mCamera.release();
    }
}

有人可以为此提供解决方案吗?日志如下:

?:??: W/?(?): java.lang.NullPointerException
?:??: W/?(?):   at com.stpl.snapshun.camera.CameraActivity.surfaceChanged(CameraActivity.java:313)
?:??: W/?(?):   at android.view.SurfaceView.updateWindow(SurfaceView.java:554)
?:??: W/?(?):   at android.view.SurfaceView.access$000(SurfaceView.java:81)
?:??: W/?(?):   at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:169)
?:??: W/?(?):   at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:671)
?:??: W/?(?):   at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1818)
?:??: W/?(?):   at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
?:??: W/?(?):   at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4212)
?:??: W/?(?):   at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
?:??: W/?(?):   at android.view.Choreographer.doCallbacks(Choreographer.java:555)
?:??: W/?(?):   at android.view.Choreographer.doFrame(Choreographer.java:525)
?:??: W/?(?):   at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
?:??: W/?(?):   at android.os.Handler.handleCallback(Handler.java:615)
?:??: W/?(?):   at android.os.Handler.dispatchMessage(Handler.java:92)
?:??: W/?(?):   at android.os.Looper.loop(Looper.java:137)
?:??: W/?(?):   at android.app.ActivityThread.main(ActivityThread.java:4745)
?:??: W/?(?):   at java.lang.reflect.Method.invokeNative(Native Method)
?:??: W/?(?):   at java.lang.reflect.Method.invoke(Method.java:511)
?:??: W/?(?):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
?:??: W/?(?):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
?:??: W/?(?):   at dalvik.system.NativeStart.main(Native Method)

我的问题是尝试访问相机参数返回 null。

提前致谢

4

5 回答 5

2

林西这是类相机视图的代码

 public void surfaceChanged(SurfaceHolder holder, int format, int width,
    int height) 
{   
    if(mPreviewRunning )
    {
        mCamera.stopPreview();
    }

    // Store width and height
    mWidth = width;
    mHeight = height;

    // Set camera parameters
    Camera.Parameters p = mCamera.getParameters();
    mCamera.setParameters(p);

    if(android.os.Build.VERSION.SDK_INT >= 8)
    {   // If API >= 8 -> rotate display...
        mCamera.setDisplayOrientation(90);
    }

    try
    {
        mCamera.setPreviewDisplay(holder);
    } catch(IOException e)
    {
        e.printStackTrace();
    }

    mCamera.startPreview();
    mPreviewRunning = true;

}
 public void surfaceCreated(final SurfaceHolder holder) 
{
    try {
        mCamera = Camera.open();
        mCamera.setPreviewDisplay(holder);
    } catch (IOException e) 
    {
        mCamera.release();
        mCamera = null;
        e.printStackTrace();
    }
}

public void surfaceDestroyed(SurfaceHolder holder) 
{
    if(mCamera != null)
    {
        mCamera.setPreviewCallback(null);
        mCamera.stopPreview();
        mCamera.setPreviewCallback(null);
        mPreviewRunning = false;
        mCamera.release();
        mCamera = null;
    }   
}

还有一件事你实现了你的课程使用

SurfaceHolder.Callback

于 2012-12-11T05:08:09.937 回答
0

我在 S3 中的相机活动也遇到了同样的问题,并且得到了修复。它对我很有用。

我不确定 。如果我说得对,您正在使用自己的相机代码使用surfaceview。

所以对于更高的android版本(<honeycomb)更好地启动默认相机。

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

特别是对于 S3(捕获的图像大小达到 4 Mb),如果您要上传到网络,最好调整它的大小并上传。因为您再次收到内存警告和崩溃,连续拍摄并上传超过 2-3 张图片。

于 2012-12-07T06:40:50.100 回答
0

这可能是由于大文件大小尝试重新调整图像大小并检查是否可以调整图像大小试试这个

public static Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {

    int width = bm.getWidth();

    int height = bm.getHeight();

    float scaleWidth = ((float) newWidth) / width;

    float scaleHeight = ((float) newHeight) / height;

    // create a matrix for the manipulation

    Matrix matrix = new Matrix();

    // resize the bit map

    matrix.postScale(scaleWidth, scaleHeight);

    // recreate the new Bitmap

    Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height,

            matrix, false);

    return resizedBitmap;
}
于 2012-12-07T08:05:28.460 回答
0

抱歉更新晚了。问题是当设备没有自动对焦时。所以你需要先检查设备是否有自动对焦: boolean focus = getPackageManager().hasSystemFeature("android.hardware.camera.autofocus");

于 2012-12-28T11:08:58.287 回答
0

德语,看看这是否有助于您理解:-

mCamera.autoFocus(myAutoFocusCallback);

/**
     * If camera having autofocus which is calling here
     */
    AutoFocusCallback myAutoFocusCallback = new AutoFocusCallback() {

        @Override
        public void onAutoFocus(boolean start, Camera arg1) {
            if (focus) {
                if (start) {
                    if (mPreviewRunning) {
                        mCamera.takePicture(myShutterCallback, mPicture,
                                mPicture);
                    }
                } else {
                    btnCamera.setEnabled(true);
                    side.setEnabled(true);
                    showToast("Could not get focus. Try again.");
                }
            } else {
                if (mPreviewRunning) {
                    mCamera.takePicture(myShutterCallback, mPicture, mPicture);
                }
            }
        }
    };


ShutterCallback myShutterCallback = new ShutterCallback() {

        @Override
        public void onShutter() {
            showToast("Picture taken!");
        }
    };
于 2013-08-08T05:27:35.787 回答