私はカメラのプレビューで何かを描こうとしています。しかし、プログラムがstartPreviewメソッドを呼び出すと、この例外がスローされます。java.lang.RuntimeException:startPreview failed。Android搭載カメラのプレビューと表面ビュー
マイコード:
public class ScanView extends SurfaceView implements SurfaceHolder.Callback {
private MySurfaceThread thread;
Camera camera;
Context context;
private boolean inPreview = false;
public class MySurfaceThread extends Thread {
private SurfaceHolder myThreadSurfaceHolder;
private ScanView myThreadSurfaceView;
private boolean myThreadRun = false;
public MySurfaceThread(SurfaceHolder surfaceHolder, ScanView surfaceView) {
myThreadSurfaceHolder = surfaceHolder;
myThreadSurfaceView = surfaceView;
}
public void setRunning(boolean b) {
myThreadRun = b;
}
@Override
public void run() {
while (myThreadRun) {
Canvas c = null;
try {
c = myThreadSurfaceHolder.lockCanvas(null);
synchronized (myThreadSurfaceHolder) {
myThreadSurfaceView.onDraw(c);
}
} finally {
if (c != null) {
myThreadSurfaceHolder.unlockCanvasAndPost(c);
}
}
}
}
}
@Override
protected void onDraw(Canvas canvas) {
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return true;
}
public ScanView(Context context) {
super(context);
this.context = context;
init();
}
private void init() {
getHolder().addCallback(this);
thread = new MySurfaceThread(getHolder(), this);
setFocusable(true); // make sure we get key events
}
public void surfaceChanged(SurfaceHolder arg0, int format, int width, int height) {
((Activity)context).setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
this.
camera.setDisplayOrientation(90);
camera.startPreview();
inPreview = true;
}
public void surfaceCreated(SurfaceHolder arg0) {
thread.setRunning(true);
thread.start();
try {
camera.setPreviewDisplay(this.getHolder());
} catch (Throwable t) {
}
}
public void surfaceDestroyed(SurfaceHolder arg0) {
boolean retry = true;
thread.setRunning(false);
while (retry) {
try {
thread.join();
retry = false;
} catch (InterruptedException e) {
}
}
}
public void onPause() {
if (inPreview) {
camera.stopPreview();
}
camera.release();
camera = null;
inPreview = false;
}
public void onResume() {
camera = Camera.open();
}
}
私はこれをどのようにしたらよいでしょうか?