Java 如何在 Android 手机上访问相机?

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

How do I access the camera on Android phones?

javaandroidandroid-emulator

提问by George

I've written a program in Java that takes in an image file and manipulates the image. Now I'm trying to access the camera so that I can take the photo and give it to the image processing program however I'm lost as to how to do this. I've read the information about the camera class and how to ask for permissions but I don't know how to actually take the photo. If anyone has any tips on where I should begin or if they know of a good tutorial I'd really appreciate it. Thanks!

我用 Java 编写了一个程序,它接收图像文件并处理图像。现在我正在尝试访问相机,以便我可以拍摄照片并将其提供给图像处理程序,但是我不知道如何执行此操作。我已经阅读了有关相机类以及如何请求权限的信息,但我不知道如何实际拍摄照片。如果有人对我应该从哪里开始有任何提示,或者如果他们知道一个好的教程,我将不胜感激。谢谢!

回答by Upvote

Google is your best friend, here are some tutorials:

谷歌是你最好的朋友,这里有一些教程:

Using the camera

使用相机

How-To Program The Google Android Camera To Take Pictures

如何编程 Google Android 相机拍照

Take Picture from Camera Emulator

从相机模拟器拍照

camera

相机

First edit your AndroidManifest.xml, add the camera permission:

首先编辑你的 AndroidManifest.xml,添加摄像头权限:

<uses-permission android:name=”android.permission.CAMERA”/>

Camera service has to be opened and closed:

必须打开和关闭相机服务:

Camera camera = Camera.open();
 //Do things with the camera
camera.release();

You can set camera settings, e.g.:

您可以设置相机设置,例如:

Camera.Parameters parameters = camera.getParameters();
parameters.setPictureFormat(PixelFormat.JPEG); 
camera.setParameters(parameters);

To take a picture:

拍照:

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.
  }
};

Do not forget to add the camera layout to your main layout xml.

不要忘记将相机布局添加到您的主布局 xml 中。

回答by Filip Spiridonov

the most important method is:

最重要的方法是:

Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
    public void onPictureTaken(byte[] imageData, Camera c) {

    }
};

This method is called when a picture is taken. Here is a good tutorial on this topic: http://www.brighthub.com/mobile/google-android/articles/43414.aspx

拍照时调用此方法。这是关于这个主题的一个很好的教程:http: //www.brighthub.com/mobile/google-android/articles/43414.aspx

hmm... or maybe you need this one:

嗯……或者你可能需要这个:

Camera mCamera;
...
public void onClick(View arg0) {
    mCamera.takePicture(null, mPictureCallback, mPictureCallback);
}

Here is one more example: http://snippets.dzone.com/posts/show/8683

这里还有一个例子:http: //snippets.dzone.com/posts/show/8683

回答by Rakesh Gondaliya

there are many ways by which u can do this.... One of the better way which i think is the short and simple is to on Button Click u can call intent which opens ur inbuilt camera view... here is the sample code...

有很多方法可以做到这一点.... ...

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);
    }

}
}

..............................................................

………………………………………………………………………………………………………………………………………………………… ………………

Go through it and, if u have any problem feel free to ask....

通过它,如果你有任何问题,请随时问......

rakesh

拉克什

回答by sd33din90

There are two methods to take photo for your android application

有两种方法可以为您的 android 应用程序拍照

1)Using Intent

1)使用意图

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) Creating a customized camera activity. For that you need following steps

2) 创建自定义的相机活动。为此,您需要以下步骤

 * Detect and Access Camera
 * Create a Preview Class
 * Build a Preview Layout 
 * Capture and Save Files
 * Release the Camera

You may also refer the following links:

您也可以参考以下链接:

http://developer.android.com/guide/topics/media/camera.htmlhttp://developer.android.com/reference/android/hardware/Camera.html

http://developer.android.com/guide/topics/media/camera.html http://developer.android.com/reference/android/hardware/Camera.html