Android 按下按钮拍摄并保存照片

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2713165/
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 07:03:05  来源:igfitidea点击:

Take and save picture on button press

androidcamera

提问by greenie

I'm creating an Android application which uses user captured images as part of a larger process. So far my XML layout has a SurfaceView and Button inside a RelativeLayout. I've managed to get the camera preview to show on the SurfaceView but I'm stuck on how to take a picture and save it when a user presses the button.

我正在创建一个 Android 应用程序,它使用用户捕获的图像作为更大进程的一部分。到目前为止,我的 XML 布局在 RelativeLayout 中有一个 SurfaceView 和 Button。我已经设法让相机预览显示在 SurfaceView 上,但我被困在如何在用户按下按钮时拍照并保存它。

My class file looks something like the CameraPreview API demo: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/CameraPreview.html

我的类文件看起来像 CameraPreview API 演示:http: //developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/CameraPreview.html

Ideally, when the button it pressed the camera should autofocus, snap a picture (with the clicky sound), save it to /data/data/app_package_structure/files/file_name.jpg, then pop up a Toast to tell the user their image has been saved.

理想情况下,当它按下按钮时,相机应该自动对焦,拍摄一张照片(带有咔哒声),将其保存到/data/data/app_package_structure/files/file_name.jpg,然后弹出一个 Toast 来告诉用户他们的图像已保存。

Any help is much appreciated :)

任何帮助深表感谢 :)

回答by Mikael Ohlson

I think CommonsWare has really already answered most of this question, but this might work for the auto focus and the shutter sound. This is a guess, since I'm not at a machine where I can compile/test any of this.

我认为 CommonsWare 确实已经回答了大部分问题,但这可能适用于自动对焦和快门声音。这是一个猜测,因为我不在可以编译/测试其中任何一个的机器上。

In your button-press-handling code, I believe you should call (possibly by message passing)

在您的按钮按下处理代码中,我相信您应该调用(可能通过消息传递)

camera.autoFocus(new Camera.AutoFocusCallback() {
  Camera.ShutterCallback shutterCallback = new Camera.ShutterCallback() {
    public void onShutter() {
      // Play your sound here.
    }
  };
  public void onAutoFocus(boolean success, Camera camera) {
    camera.takePicture(shutterCallback, null, photoCallback);
  }
});  

where camerais your camera object, and photoCallbackis the same as in CommonsWare's example.

camera您的相机对象在哪里,与photoCallbackCommonsWare 示例中的相同。

Exactly what is it that you are stuck on?

究竟是什么让你被困住了?

Oh, and don't forget to add the <uses-feature>tag android.hardware.camera.autofocus. :)

哦,别忘了添加<uses-feature>标签android.hardware.camera.autofocus。:)

回答by CommonsWare

Here is a sample applicationthat handles the take-a-picture-and-save-it part. Auto-focus, clicky, Toast, and saving to the app-local file store vs. the SD card are left as exercises for the student. :-)

这是一个处理拍照并保存部分的示例应用程序。自动对焦、点击、Toast保存到应用程序本地文件存储与 SD 卡作为练习留给学生。:-)