【发布时间】:2014-09-27 07:37:01
【问题描述】:
大家好,我想弄清楚如何通过按下按钮来拍照,但没有显示任何预览。这个想法是,我想要拍摄并保存一张照片,但在之前或之后没有照片的视觉预览。到目前为止,我能够毫无问题地获取用于拍照并将它们保存到磁盘的代码,但如果没有表面视图或预览,我似乎无法做到这一点。
这是我的一些代码:
主要活动:
public class MainActivity extends Activity implements OnClickListener {
private Camera cameraObject;
private ShowCamera showCamera;
private Button NOPE;
//Check if camera is avail:
public static Camera isCameraAvailiable(){
Camera object = null;
try {
object = Camera.open();
L.m("Camera Open");
} catch (Exception e) {
L.m(e.toString());
} return object;
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Opens up the camera
cameraObject = isCameraAvailiable();
//Sets the resolution for the camera (Excluded from code here)
setCameraResolution();
//Button for taking photos
NOPE = (Button) findViewById(R.id.button_capture);
NOPE.setOnClickListener(this);
//THIS SECTION OF CODE HERE I can't get it to work without it as this creates a view/ preview for the camera
showCamera = new ShowCamera(this, cameraObject);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(showCamera);
}
public void snapIt(View view){
cameraObject.takePicture(null, null, new PhotoHandler(getApplicationContext()));
}
public void onClick(View view) {
switch (view.getId()){
case R.id.button_capture:
snapIt(view);
}
}
}
照片处理类:
public class PhotoHandler implements PictureCallback {
private final Context context;
public PhotoHandler(Context context) {
this.context = context;
}
public void onPictureTaken(byte[] data, Camera camera) {
File pictureFileDir = getDir();
if (!pictureFileDir.exists() && !pictureFileDir.mkdirs()) {
//I removed unnecessary code here, but this is where I write to disk, which works fine.
}
}
我遇到的问题是,除非我有关于 preview.addView(showCamera); 的代码,否则我实际上无法通过相机拍照。 ShowCamera 类只是添加了一个表面视图,以便在拍摄时查看图片:
public class ShowCamera extends SurfaceView implements SurfaceHolder.Callback
有人有什么想法吗?这个可以吗?
有人在这里问了一个非常相似的问题:Taking picture without SurfaceView or without Photo Intent Activity,但没有任何成功。我想我和他们走的是相似的道路。
【问题讨论】:
标签: android image android-intent camera