在按钮上按下并保存图片 [英] Take and save picture on button press

查看:210
本文介绍了在按钮上按下并保存图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



理想情况下,当按下按钮的按钮应该自动对焦,拍摄一张图片(带有点击声),保存到 / data / data / app_package_structure / files / file_name



任何帮助都非常感谢:)

解决方案

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



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

  camera.autoFocus(new Camera.AutoFocusCallback(){
Camera.ShutterCallback shutterCallback = new Camera.ShutterCallback(){
public void onShutter(){
//在这里播放声音
}
};
public void onAutoFocus成功,相机相机){
camera.takePicture(shutterCallback,null,photoCallback);
}
});

其中相机是您的相机对象, photoCallback 与CommonsWare的例子相同。



你被困在什么? >

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


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.

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

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.

Any help is much appreciated :)

解决方案

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.

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 camera is your camera object, and photoCallback is the same as in CommonsWare's example.

Exactly what is it that you are stuck on?

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

这篇关于在按钮上按下并保存图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆