gpt4 book ai didi

Android:带有 SurfaceView 的相机

转载 作者:行者123 更新时间:2023-11-29 01:52:53 26 4
gpt4 key购买 nike

我正在尝试将 Android 的相机与 SurfaceView 一起使用,以便用户可以预览相机。我遵循了 StackOverflow 上的各种教程,最后找到了 Google 自己的教程,但我的应用程序崩溃了。这是我的代码。我正在使用 Eclipse 的 Android 虚拟设备、模拟相机(我认为这不会影响任何东西)和分配的 100mb SD 卡空间。

有两个类,CameraPreview 对象和 CameraActivity。

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback{

private SurfaceHolder mHolder;
private Camera mCamera;
private final String TAG = "TAG";

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);
// deprecated setting, but required on Android versions prior to 3.0
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

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.
}

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.

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());
}
}
}


public class CameraActivity extends Activity{
private Camera mCamera;
private CameraPreview mPreview;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Create an instance of Camera
mCamera = getCameraInstance();

// Create our Preview view and set it as the content of our activity.
mPreview = new CameraPreview(this, mCamera);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(mPreview);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

public Camera getCameraInstance(){
Camera c = null;
try {
c = Camera.open(); // attempt to get a Camera instance
}
catch (Exception e){
// Camera is not available (in use or does not exist)
}
return c; // returns null if camera is unavailable
}
}

06-04 14:28:07.726: W/dalvikvm(6115): threadid=1: 线程退出并出现未捕获的异常 (group=0x40a71930)06-04 14:28:07.956: E/AndroidRuntime(6115): 致命异常: main06-04 14:28:07.956: E/AndroidRuntime(6115): java.lang.RuntimeException: 无法启动 Activity ComponentInfo{com.example.mobilequote/com.example.mobilequote.CameraActivity}: java.lang.NullPointerException06-04 14:28:07.956: E/AndroidRuntime (6115): 在 android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2180)06-04 14:28:07.956: E/AndroidRuntime (6115): 在 android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2230)06-04 14:28:07.956: E/AndroidRuntime(6115): 在 android.app.ActivityThread.access$600(ActivityThread.java:141)06-04 14:28:07.956: E/AndroidRuntime (6115): 在 android.app.ActivityThread$H.handleMessage (ActivityThread.java:1234)06-04 14:28:07.956: E/AndroidRuntime (6115): 在 android.os.Handler.dispatchMessage (Handler.java:99)06-04 14:28:07.956: E/AndroidRuntime(6115): 在 android.os.Looper.loop(Looper.java:137)06-04 14:28:07.956: E/AndroidRuntime(6115): 在 android.app.ActivityThread.main(ActivityThread.java:5041)06-04 14:28:07.956:E/AndroidRuntime(6115):在 java.lang.reflect.Method.invokeNative( native 方法)06-04 14:28:07.956: E/AndroidRuntime(6115): 在 java.lang.reflect.Method.invoke(Method.java:511)06-04 14:28:07.956: E/AndroidRuntime(6115): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)06-04 14:28:07.956: E/AndroidRuntime (6115): 在 com.android.internal.os.ZygoteInit.main (ZygoteInit.java:560)06-04 14:28:07.956:E/AndroidRuntime(6115):在 dalvik.system.NativeStart.main( native 方法)06-04 14:28:07.956: E/AndroidRuntime(6115): 由 java.lang.NullPointerException 引起06-04 14:28:07.956: E/AndroidRuntime(6115): 在 com.example.mobilequote.CameraActivity.onCreate(CameraActivity.java:24)06-04 14:28:07.956: E/AndroidRuntime(6115): 在 android.app.Activity.performCreate(Activity.java:5104)06-04 14:28:07.956: E/AndroidRuntime (6115): 在 android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1080)06-04 14:28:07.956: E/AndroidRuntime (6115): 在 android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2144)06-04 14:28:07.956: E/AndroidRuntime(6115): ... 11 更多

CameraActivity.java:24 行是 preview.addView(mPreview);

最佳答案

请参阅我对模拟器的评论。但是你的应用崩溃的原因是因为你捕捉到相机无法打开的异常并返回null。然后在您的 CameraPreview 类中将此空值保存到“mCamera”,然后在 surfaceCreated 中取消引用它:

mCamera.setPreviewDisplay(holder);

所以你最初的问题是为什么它会崩溃......它崩溃是因为你没有很好地处理错误情况。

关于Android:带有 SurfaceView 的相机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16907005/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com