Android - 无需预览即可拍照

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25754597/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:42:16  来源:igfitidea点击:

Android - Take picture without preview

androidandroid-camerasurfaceview

提问by Eyal

I am trying to take a picture without preview, immediately when my application starts running and after that to save the picture in new folder - "pictures123", in the root folder. Could someone please tell me what's wrong in my code?

我正在尝试在不预览的情况下拍摄照片,当我的应用程序开始运行时立即拍照,然后将照片保存在根文件夹中的新文件夹 - “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();

    }
};

permissions:

权限:

<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" />

采纳答案by fadden

You can't take a picture without a preview, but you don't have to show the preview on screen. You can direct the output to a SurfaceTexture instead (API 11+).

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

See this answerfor more details.

有关更多详细信息,请参阅此答案

回答by hzitoun

If you are using Camera 2 API, to take picture secretly without preview or launching device's camera app you can check my project on github.

如果您使用的是Camera 2 API,要在不预览或启动设备的相机应用程序的情况下秘密拍照,您可以在 github 上查看我的项目

回答by ashoke

I think the main problem is that you are calling takePicture immediately after startPreview which actually takes time to complete its setup. So adding a delay between those two could temporarily fix this issue, look here for more details.

我认为主要问题是您在 startPreview 之后立即调用 takePicture ,这实际上需要时间来完成其设置。因此,在这两者之间添加延迟可以暂时解决此问题,请查看此处了解更多详细信息

回答by yushulx

It's impossible to take a picture without preview. You should read the Android online reference: http://developer.android.com/reference/android/hardware/Camera.html#takePicture.

没有预览是不可能拍照的。您应该阅读 Android 在线参考:http: //developer.android.com/reference/android/hardware/Camera.html#takePicture

Note: This method is only valid when preview is active (after startPreview()). Preview will be stopped after the image is taken; callers must call startPreview() again if they want to re-start preview or take more pictures. This should not be called between start() and stop().

注意:此方法仅在预览处于活动状态时有效(在 startPreview() 之后)。拍摄图像后将停止预览;如果调用者想要重新开始预览或拍摄更多照片,则必须再次调用 startPreview()。这不应在 start() 和 stop() 之间调用。