好的,我在实现无需按下按钮即可捕获图像的相机应用程序时遇到了麻烦。当前是一个工作的相机应用程序,但是当下面的代码被注释掉时:
buttonClick = (Button) findViewById(R.id.buttonClick);
buttonClick.setOnClickListener(new OnClickListener() {
public void onClick(View v) { // <5>
(1) preview.camera.takePicture(shutterCallback, rawCallback, jpegCallback);
}
});*/
并替换为只调用 (1) 的循环,预览永远不可见并导致应用程序关闭。预览类如下:
import java.io.IOException;
import android.content.Context;
import android.hardware.Camera;
import android.hardware.Camera.PreviewCallback;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
class Preview extends SurfaceView implements SurfaceHolder.Callback { //
<1>
private static final String TAG = "Preview";
SurfaceHolder mHolder; // <2>
public Camera camera; // <3>
Preview(Context context) {
super(context);
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder(); // <4>
mHolder.addCallback(this); // <5>
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); // <6>
}
// Called once the holder is ready
public void surfaceCreated(SurfaceHolder holder) { // <7>
// The Surface has been created, acquire the camera and tell it where
// to draw.
camera = Camera.open(); // <8>
try {
camera.setPreviewDisplay(holder); // <9>
camera.setPreviewCallback(new PreviewCallback() { // <10>
// Called for each frame previewed
public void onPreviewFrame(byte[] data, Camera camera) { // <11>
Log.d(TAG, "onPreviewFrame called at: " + System.currentTimeMillis());
Preview.this.invalidate(); // <12>
}
});
} catch (IOException e) { // <13>
e.printStackTrace();
}
}
// Called when the holder is destroyed
public void surfaceDestroyed(SurfaceHolder holder) { // <14>
camera.stopPreview();
camera = null;
}
// Called when holder has changed
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { // <15>
camera.startPreview();
}
}
关于从这里去哪里以及为什么会发生这种情况的任何想法?