gpt4 book ai didi

Android - 无需预览即可拍照

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:05:14 26 4
gpt4 key购买 nike

我正在尝试在没有预览的情况下拍照,当我的应用程序开始运行时立即拍照,然后将照片保存在根文件夹中的新文件夹 - “pictures123”中。有人可以告诉我我的代码有什么问题吗?

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
File directory = new File(Environment.getExternalStorageDirectory() + "/pictures123/");

if (!directory.exists()) {
directory.mkdir();
}

Camera camera = Camera.open(0);
Camera.Parameters parameters = camera.getParameters();
parameters.setPictureFormat(ImageFormat.JPEG);
camera.setParameters(parameters);
SurfaceView mview = new SurfaceView(getBaseContext());
camera.setPreviewDisplay(mview.getHolder());
camera.setPreviewDisplay(null);
camera.startPreview();
camera.takePicture(null,null,photoCallback);
camera.stopPreview();
}

Camera.PictureCallback photoCallback=new Camera.PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {

try {
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/pictures123");
File file = new File (myDir, "pic1.jpeg");
FileOutputStream out = new FileOutputStream(file);
out.write(data);
out.flush();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();

} catch (IOException e) {
e.printStackTrace();

} catch (Exception e)
{
e.printStackTrace();
}
finish();

}
};

权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />

最佳答案

您不能在没有预览的情况下拍照,但您不必在屏幕上显示预览。您可以将输出定向到 SurfaceTexture(API 11+)。

参见 this answer了解更多详情。

关于Android - 无需预览即可拍照,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25754597/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com