我的应用程序具有相机功能。它适用于所有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。
提前致谢
答案 0 :(得分:2)
Lincy这是类相机视图代码
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
答案 1 :(得分:0)
我在S3中的相机活动也遇到了同样的问题,并得到了修复。它对我有用。
我不确定。如果我正确使用它,你使用自己的代码来使用surfaceview。
因此对于更高版本的Android版本(< honeycomb)更好地启动默认摄像头。
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
并且特别针对S3(捕获的图像大小超过4 Mb),如果您正在上传到网络,请更好地调整大小并上传。因为你得到了内存警告并再次崩溃,所以连续拍摄和上传的图片超过2-3张。
答案 2 :(得分: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;
}
答案 3 :(得分:0)
对于迟到的更新感到抱歉。问题是设备没有自动对焦。因此,您需要首先检查设备是否具有自动聚焦:boolean focus = getPackageManager()。hasSystemFeature(“android.hardware.camera.autofocus”);
答案 4 :(得分: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!");
}
};