本文介绍了相机startPreview失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在创建一个相机应用,但我在 startPreview
上遇到问题,它会发送给我:
java.lang.RuntimeException:startPreview failed
这里是我的相机 >活动:
public class CameraActivity extends Activity {
private Camera mCamera;
private CameraPreview mPreview;
private Target_Frame targetFrame
@Override
protected void onCreate(Bundle savedInstanceState){
// TODO自动生成方法存根
super.onCreate(savedInstanceState);
setContentView(R.layout.camera_layout);
mCamera = getCameraInstance();
mPreview = new CameraPreview(this,mCamera);
FrameLayout preview =(FrameLayout)findViewById(R.id.camera_preview);
preview.addView(mPreview);
}
/ **检查此设备是否有相机只有未在清单中指定* /
public boolean checkCameraHardware(Context context){
if(context.getPackageManager()。hasSystemFeature(PackageManager.FEATURE_CAMERA)){
//这个设备有一个摄像头
return true;
} else {
//此设备上没有摄像机
return false;
}
}
/ **一种获取Camera对象实例的安全方法。 * /
public static Camera getCameraInstance(){
Camera c = null;
try {
c = Camera.open(); //尝试获取一个Camera实例
}
catch(Exception e){
//相机不可用(正在使用或不存在)
}
return c; //如果相机不可用返回null
}
/ **检查设备是否有闪存* /
public boolean checkFlash(Context context){
if context.getPackageManager()。hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)){
//设备有flash
return true;
} else {
// no flash
return false;
}
}
@Override
protected void onDestroy(){
// TODO自动生成方法存根
super.onDestroy();
releaseCamera();
}
@Override
protected void onPause(){
// TODO自动生成方法存根
super.onPause();
releaseCamera();
}
@Override
protected void onResume(){
// TODO自动生成方法存根
super.onResume();
//测试我是否要把所有的代码放在onCreate中
if(mCamera!= null){
return;
}
mCamera = getCameraInstance();
if(mPreview!= null){
return;
}
mPreview = new CameraPreview(this,mCamera);
FrameLayout preview =(FrameLayout)findViewById(R.id.camera_preview);
preview.addView(mPreview);
}
private void releaseCamera(){
if(mCamera!= null){
mCamera.release (); //为其他应用程序释放摄像头
mCamera = null;
}
}}
em>代码:
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private static final String TAG =CameraPreview ;
private SurfaceHolder mHolder;
私人相机mCamera;
public CameraPreview(Context context,Camera camera){
super(context);
mCamera = camera;
//安装一个SurfaceHolder.Callback,这样当
//底层表面被创建和销毁的时候我们会收到通知。
mHolder = getHolder();
mHolder.addCallback(this);
//弃用设置,但在3.0之前的Android版本中需要
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceCreated(SurfaceHolder holder){
// Surface已经创建,现在告诉摄像机在哪里绘制预览。
try {
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
} catch(IOException e){
Log.d(TAG,Error setting camera preview:+ e.getMessage());
}
}
public void surfaceDestroyed(SurfaceHolder holder){
//空。请小心在您的活动中释放相机预览。
}
public void surfaceChanged(SurfaceHolder holder,int format,int w,int h){
//如果你的预览可以改变或旋转,在这里处理这些事件。
//确保在调整大小或重新格式化之前停止预览。
if(mHolder.getSurface()== null){
//预览曲面不存在
return;
}
//在进行更改之前停止预览
try {
mCamera.stopPreview();
} catch(Exception e){
// ignore:试图停止不存在的预览
}
//设置预览大小,旋转或
//重新格式化更改
参数parameters = mCamera.getParameters();
parameter.setPreviewSize(w,h);
mCamera.setParameters(parameters);
//用新设置开始预览
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
} catch(Exception e){
Log.d(TAG,启动相机预览时出错:+ e.getMessage());
}
}
}
:
12-01 13:17:01.135:E / AndroidRuntime(1161):FATAL EXCEPTION:main
12-01 13:17:01.135:E / AndroidRuntime(1161):java.lang.RuntimeException:startPreview
12-01 13:17:01.135: at com.example.prueba.CameraPreview.surfaceCreated(CameraPreview.java:36)
解决方案
我已经解决了删除surfaceChanged中的一些行
public void surfaceChanged(SurfaceHolder holder,int format,int w ,int h){
//如果您的预览可以更改或旋转,请在这里处理这些事件。
//确保在调整大小或重新格式化之前停止预览。
Log.d(Function,surfaceChanged iniciado);
if(mHolder.getSurface()== null){
//预览曲面不存在
return;
}
//在进行更改之前停止预览
try {
mCamera.stopPreview();
} catch(Exception e){
// ignore:试图停止不存在的预览
}
//设置预览大小,旋转或
//重新格式化改变这里
//用新设置开始预览
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
} catch(Exception e){
Log.d(TAG,启动相机预览时出错:+ e.getMessage());
}
}
所以错误必须在这些行之一:
参数parameters = mCamera.getParameters();
parameter.setPreviewSize(w,h);
mCamera.setParameters(parameters);有人可以解释一下这些行中出了什么问题? I am creating a camera app but I have problems on startPreview
, it sends me:
java.lang.RuntimeException: startPreview failed
here is my camera Activity:
public class CameraActivity extends Activity {
private Camera mCamera;
private CameraPreview mPreview;
private Target_Frame targetFrame;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.camera_layout);
mCamera=getCameraInstance();
mPreview=new CameraPreview(this, mCamera);
FrameLayout preview=(FrameLayout)findViewById(R.id.camera_preview);
preview.addView(mPreview);
}
/** Check if this device has a camera only if not specified in the manifest */
public boolean checkCameraHardware(Context context) {
if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
// this device has a camera
return true;
} else {
// no camera on this device
return false;
}
}
/** A safe way to get an instance of the Camera object. */
public static 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
}
/**Check if the device has flash*/
public boolean checkFlash(Context context){
if(context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)){
//the device has flash
return true;
}else{
//no flash
return false;
}
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
releaseCamera();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
releaseCamera();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
//Test if i have to put all this code like in onCreate
if(mCamera!=null){
return;
}
mCamera=getCameraInstance();
if(mPreview!=null){
return;
}
mPreview=new CameraPreview(this, mCamera);
FrameLayout preview=(FrameLayout)findViewById(R.id.camera_preview);
preview.addView(mPreview);
}
private void releaseCamera(){
if (mCamera != null){
mCamera.release(); // release the camera for other applications
mCamera = null;
}
}}
And here is my SurfaceView code:
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private static final String TAG = "CameraPreview";
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);
// 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
Parameters parameters= mCamera.getParameters();
parameters.setPreviewSize(w, h);
mCamera.setParameters(parameters);
// start preview with new settings
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
} catch (Exception e){
Log.d(TAG, "Error starting camera preview: " + e.getMessage());
}
}
}
And here is my error log:
12-01 13:17:01.135: E/AndroidRuntime(1161): FATAL EXCEPTION: main
12-01 13:17:01.135: E/AndroidRuntime(1161): java.lang.RuntimeException: startPreview
12-01 13:17:01.135: E/AndroidRuntime(1161): at com.example.prueba.CameraPreview.surfaceCreated(CameraPreview.java:36)
解决方案 I have solved deleting some lines in surfaceChanged
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());
}
}
So the error must be in i one of these lines:
Parameters parameters= mCamera.getParameters();
parameters.setPreviewSize(w, h);
mCamera.setParameters(parameters);
Someone could explain me what was wrong in those lines?
这篇关于相机startPreview失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!