大家好,我正在尝试弄清楚如何通过按下按钮来拍照,而不显示任何预览。我的想法是,我想要拍摄并保存照片,但之前或之后没有照片的视觉预览。到目前为止,我能够获取拍照并将其保存到磁盘的代码,没有任何问题,但如果没有表面 View 或预览,我似乎无法做到这一点。
这是我的一些代码:
主要 Activity :
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 类只是添加表面 View 以在拍摄照片时查看照片的类:
public class ShowCamera extends SurfaceView implements SurfaceHolder.Callback
大家有什么想法吗?这可以吗?
有人在这里问了一个极其相似的问题:Taking picture without SurfaceView or without Photo Intent Activity ,但没有任何成功。我想我和他们走的是相似的道路。
最佳答案
我有同样的问题,我通过在 SurficeView 上放置新 View 来解决它,这样就看不到 surficeView 了。我花了相当长的时间寻找其他解决方案但没有成功。
关于android - 如何在Android中没有Intent且没有任何 View 窗口的情况下拍照,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26072438/