当前位置: 代码迷 >> Android >> android中关于camera开发的一些有关问题
  详细解决方案

android中关于camera开发的一些有关问题

热度:50   发布时间:2016-05-01 15:07:10.0
android中关于camera开发的一些问题
最近开发android中camera程序中遇到的问题一直未能解决,求各位大侠帮帮忙
代码如下:
public class CameraTActivity extends Activity implements OnClickListener,SurfaceHolder.Callback{
  /** Called when the activity is first created. */
private Button camera_start,camera_pause;
public Camera camera ;
public Camera.Parameters parameters ;

private SurfaceHolder surfaceHolder;
private SurfaceView surfaceView;
private SurfaceView camera_play_surfaceview;
private int screenWidth,screenHeight;
  @Override
  public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
   
  initView();
  System.out.println("start---------");
  }
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
try {
startCamera();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
System.out.println("create");
if(camera==null){
camera = Camera.open();
}
System.out.println("camera start");
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
if(camera!=null){
camera.stopPreview();
camera.release();

}
}
public void initView(){

surfaceView = (SurfaceView)findViewById(R.id.surfaceview);
surfaceHolder = surfaceView.getHolder();
  surfaceHolder.addCallback(this);
   
}
/**
* Creates a new Camera object to access the first back-facing camera on the device. 
* If the device does not have a back-facing camera, this returns null.
* @throws IOException */
private void startCamera() throws IOException{


parameters = camera.getParameters();
screenWidth = surfaceView.getWidth();
screenHeight = surfaceView.getHeight();
parameters.setPreviewSize(screenWidth, screenHeight); // 设置预览照片的大小
parameters.setPreviewFpsRange(60, 75); // 每秒显示20~30帧
parameters.setPictureFormat(ImageFormat.NV21); // 设置图片格式
parameters.setPictureSize(screenWidth, screenHeight); // 设置照片的大小
// camera.setParameters(parameters); // android2.3.3以后不需要此行代码
camera.setPreviewDisplay(surfaceHolder); // 通过SurfaceView显示取景画面
camera.setPreviewCallback(cPreview); // 设置回调的类
camera.startPreview(); // 开始预览
camera.autoFocus(null); // 自动对焦
}
}

log信息如下:

01-02 01:21:13.380: WARN/System.err(3155): java.lang.RuntimeException: startPreview failed
01-02 01:21:13.390: WARN/System.err(3155): at android.hardware.Camera.startPreview(Native Method)
01-02 01:21:13.390: WARN/System.err(3155): at com.camera.CameraTActivity.startCamera(CameraTActivity.java:100)
01-02 01:21:13.390: WARN/System.err(3155): at com.camera.CameraTActivity.surfaceChanged(CameraTActivity.java:40)
01-02 01:21:13.390: WARN/System.err(3155): at android.view.SurfaceView.updateWindow(SurfaceView.java:549)
01-02 01:21:13.390: WARN/System.err(3155): at android.view.SurfaceView.dispatchDraw(SurfaceView.java:348)
01-02 01:21:13.390: WARN/System.err(3155): at android.view.ViewGroup.drawChild(ViewGroup.java:1645)
01-02 01:21:13.390: WARN/System.err(3155): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1374)
01-02 01:21:13.390: WARN/System.err(3155): at android.view.ViewGroup.drawChild(ViewGroup.java:1645)
01-02 01:21:13.390: WARN/System.err(3155): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1374)
01-02 01:21:13.390: WARN/System.err(3155): at android.view.View.draw(View.java:7165)
  相关解决方案