我用Java编写了一个程序,它接收一个图像文件并操纵图像。现在我正在尝试访问相机,以便我可以拍摄照片并将其提供给图像处理程序,但是我已经迷失了如何做到这一点。我已经阅读了有关相机类的信息以及如何申请权限,但我不知道如何拍照。如果有人对我应该从哪里开始有任何提示,或者如果他们知道一个好的教程,我会非常感激。谢谢!
答案 0 :(得分:7)
谷歌是你最好的朋友,这里有一些教程:
How-To Program The Google Android Camera To Take Pictures
Take Picture from Camera Emulator
首先编辑您的AndroidManifest.xml,添加相机权限:
<uses-permission android:name=”android.permission.CAMERA”/>
必须打开和关闭相机服务:
Camera camera = Camera.open();
//Do things with the camera
camera.release();
您可以设置相机设置,例如:
Camera.Parameters parameters = camera.getParameters();
parameters.setPictureFormat(PixelFormat.JPEG);
camera.setParameters(parameters);
拍照:
private void takePicture() {
camera.takePicture(shutterCallback, rawCallback, jpegCallback);
}
ShutterCallback shutterCallback = new ShutterCallback() {
public void onShutter() {
// TODO Do something when the shutter closes.
}
};
PictureCallback rawCallback = new PictureCallback() {
public void onPictureTaken(byte[] _data, Camera _camera) {
// TODO Do something with the image RAW data.
}
};
PictureCallback jpegCallback = new PictureCallback() {
public void onPictureTaken(byte[] _data, Camera _camera) {
// TODO Do something with the image JPEG data.
}
};
不要忘记将相机布局添加到主布局xml。
答案 1 :(得分:6)
你有很多方法可以做到这一点.... 我认为最好的方法之一就是简短而简单就是按下按钮你可以调用意图打开你内置的摄像头视图......这里是示例代码......
public class CameraDemo extends Activity {
Button ButtonClick;
int CAMERA_PIC_REQUEST = 2;
int TAKE_PICTURE=0;
Camera camera;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ButtonClick =(Button) findViewById(R.id.Camera);
ButtonClick.setOnClickListener(new OnClickListener (){
@Override
public void onClick(View view)
{
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if( requestCode == CAMERA_PIC_REQUEST)
{
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ImageView image =(ImageView) findViewById(R.id.PhotoCaptured);
image.setImageBitmap(thumbnail);
}
else
{
Toast.makeText(demo.this, "Picture NOt taken", Toast.LENGTH_LONG);
}
}
}
............................................... ...............
通过它,如果你有任何问题,请随时提出....
勒凯什
答案 2 :(得分:2)
为Android应用程序拍摄照片有两种方法
1)使用Intent
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
// start the image capture Intent
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
2)创建自定义相机活动。为此,您需要执行以下步骤
* Detect and Access Camera
* Create a Preview Class
* Build a Preview Layout
* Capture and Save Files
* Release the Camera
您还可以参考以下链接:
http://developer.android.com/guide/topics/media/camera.html http://developer.android.com/reference/android/hardware/Camera.html
答案 3 :(得分:0)
最重要的方法是:
Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
public void onPictureTaken(byte[] imageData, Camera c) {
}
};
拍摄照片时调用此方法。 这是一个关于这个主题的好教程:http://www.brighthub.com/mobile/google-android/articles/43414.aspx
嗯......或者你可能需要这个:Camera mCamera;
...
public void onClick(View arg0) {
mCamera.takePicture(null, mPictureCallback, mPictureCallback);
}