我正在开发一个Android应用程序,它需要在应用程序屏幕的一半显示摄像头视图。我可以在Android 4.0设备上成功地做到这一点,但当我试图在Nexus One上运行它时,它崩溃了,尽管minSDK版本已设置为2.2。这是我的摄像头预览类。
package com.example.locationtest;
import java.io.IOException;
import android.content.Context;
import android.hardware.Camera;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private static final String TAG = "error";
private SurfaceHolder mHolder;
private Camera mCamera;
public CameraPreview(Context context, Camera camera) {
super(context);
mCamera = camera;
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
}
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());
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
// empty. Take care of releasing the Camera preview in your activity.
mCamera.release();
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// If your preview can change or rotate, take care of those events here.
// Make sure to stop the preview before resizing or reformatting it.
Log.d("Function", "surfaceChanged iniciado");
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
}
// set preview size and make any resize, rotate or
// reformatting changes here
// start preview with new settings
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
} catch (Exception e){
Log.d(TAG, "Error starting camera preview: " + e.getMessage());
}
}
}
它给出以下logcat:
01-05 18:42:03.540: E/AndroidRuntime(583): FATAL EXCEPTION: main
01-05 18:42:03.540: E/AndroidRuntime(583): java.lang.RuntimeException: startPreview failed
01-05 18:42:03.540: E/AndroidRuntime(583): at android.hardware.Camera.startPreview(Native Method)
01-05 18:42:03.540: E/AndroidRuntime(583): at com.example.locationtest.CameraPreview.surfaceCreated(CameraPreview.java:31)
01-05 18:42:03.540: E/AndroidRuntime(583): at android.view.SurfaceView.updateWindow(SurfaceView.java:543)
01-05 18:42:03.540: E/AndroidRuntime(583): at android.view.SurfaceView.dispatchDraw(SurfaceView.java:348)
01-05 18:42:03.540: E/AndroidRuntime(583): at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
01-05 18:42:03.540: E/AndroidRuntime(583): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
01-05 18:42:03.540: E/AndroidRuntime(583): at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
01-05 18:42:03.540: E/AndroidRuntime(583): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
01-05 18:42:03.540: E/AndroidRuntime(583): at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
01-05 18:42:03.540: E/AndroidRuntime(583): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
01-05 18:42:03.540: E/AndroidRuntime(583): at android.view.View.draw(View.java:6883)
01-05 18:42:03.540: E/AndroidRuntime(583): at android.widget.FrameLayout.draw(FrameLayout.java:357)
01-05 18:42:03.540: E/AndroidRuntime(583): at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
01-05 18:42:03.540: E/AndroidRuntime(583): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
01-05 18:42:03.540: E/AndroidRuntime(583): at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
01-05 18:42:03.540: E/AndroidRuntime(583): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
01-05 18:42:03.540: E/AndroidRuntime(583): at android.view.View.draw(View.java:6883)
01-05 18:42:03.540: E/AndroidRuntime(583): at android.widget.FrameLayout.draw(FrameLayout.java:357)
01-05 18:42:03.540: E/AndroidRuntime(583): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1862)
01-05 18:42:03.540: E/AndroidRuntime(583): at android.view.ViewRoot.draw(ViewRoot.java:1522)
01-05 18:42:03.540: E/AndroidRuntime(583): at android.view.ViewRoot.performTraversals(ViewRoot.java:1258)
01-05 18:42:03.540: E/AndroidRuntime(583): at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
01-05 18:42:03.540: E/AndroidRuntime(583): at android.os.Handler.dispatchMessage(Handler.java:99)
01-05 18:42:03.540: E/AndroidRuntime(583): at android.os.Looper.loop(Looper.java:130)
01-05 18:42:03.540: E/AndroidRuntime(583): at android.app.ActivityThread.main(ActivityThread.java:3683)
01-05 18:42:03.540: E/AndroidRuntime(583): at java.lang.reflect.Method.invokeNative(Native Method)
01-05 18:42:03.540: E/AndroidRuntime(583): at java.lang.reflect.Method.invoke(Method.java:507)
01-05 18:42:03.540: E/AndroidRuntime(583): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-05 18:42:03.540: E/AndroidRuntime(583): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-05 18:42:03.540: E/AndroidRuntime(583): at dalvik.system.NativeStart.main(Native Method)
2条答案
按热度按时间rta7y2nd1#
用Thread.Sleep(2000)添加一个新线程,然后开始预览,这样相机就会显示捕捉到的图像2秒钟,并会重新启动相机。
kupeojn62#
遇到同样的问题,请试试这个: