Android Camera的使用 (二)

一、前言
在我们使用Camera时我们应该首先考虑以下几个注意事项
1、我们的设备必须要有摄像头,以及我们应该在AndroidManifest.xml中将需要用到的权限必须加上。
2、我们的应用程序用Camera来干什么?能否考虑使用系统自带的Camera应用,还是需要自定义一个相机。
3、我们的媒体文件怎样存储,是私有,还是共享?以及我们的相册应用可以查看这些文件吗?
我们接下来就针对上面的问题进行讨论。

二、清单文件的声明
1、Camera 权限

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

2、Camera Features

<uses-feature android:name="android.hardware.camera" />

如果使用只使用系统相机可以不声明这个。

3、若果应用程序 不需要 设备提供声明的特性也能正确运行的话,可以这样声明

<uses-feature android:name="android.hardware.camera" android:required="false" />

4、存储权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

5、录制需要的权限

<uses-permission android:name="android.permission.RECORD_AUDIO" />

6、如果拍摄的图片需要有GPS信息

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

若果我们还依赖一些硬件特性 需继续声明< use- fuature >
比如如果我们需要自动对焦,则需加入

  <uses-feature android:name="android.hardware.camera.autofocus"></uses-feature>

Camera的其他 硬件特性请参考:
https://developer.android.com/guide/topics/manifest/uses-feature-element.html#hw-features

三、使用系统的相机应用
一个最快的方法使用拍照和摄像同能就是通过Intent来调用已经存在的相机应用来进行拍照和录像。调用存在的相机应用之后三步
1、创建一个Intent加入Action
MediaStore.ACTION_IMAGE_CAPTURE————-请求照相
MediaStore.ACTION_VIDEO_CAPTURE————-请求录像
2、使用 startActivityForResult() 调用相机应用
3、在 onActivityResult() 方法里接收数据

示例:
1、调用系统相机拍照

private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
private Uri fileUri;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // create Intent to take a picture and return control to the calling application
    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);
}

其中MediaStore.EXTRA_OUTPUT - - 它用来指定文件的存储位置,这个选项是个可选项,但强烈建议使用,如果没有设置,则相机应用则将文件保存在一个默认的位置,以及一个默认的名字。将必须在 OnActivityResult 使用Intent.getData()来获取数据。

getOutputMediaFileUri(MEDIA_TYPE_IMAGE) 是一个创建 Uri的工具方法

public static final int MEDIA_TYPE_IMAGE = 1;
public static final int MEDIA_TYPE_VIDEO = 2;

/** Create a file Uri for saving an image or video */
private static Uri getOutputMediaFileUri(int type){
      return Uri.fromFile(getOutputMediaFile(type));
}

/** Create a File for saving an image or video */
private static File getOutputMediaFile(int type){
    // To be safe, you should check that the SDCard is mounted
    // using Environment.getExternalStorageState() before doing this.

    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
              Environment.DIRECTORY_PICTURES), "MyCameraApp");
    // This location works best if you want the created images to be shared
    // between applications and persist after your app has been uninstalled.

    // Create the storage directory if it does not exist
    if (! mediaStorageDir.exists()){
        if (! mediaStorageDir.mkdirs()){
            Log.d("MyCameraApp", "failed to create directory");
            return null;
        }
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE){
        mediaFile = new File(mediaStorageDir.getPath() + File.separator +
        "IMG_"+ timeStamp + ".jpg");
    } else if(type == MEDIA_TYPE_VIDEO) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator +
        "VID_"+ timeStamp + ".mp4");
    } else {
        return null;
    }

    return mediaFile;
}

2、使用系统应用录像:

private static final int CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE = 200;
private Uri fileUri;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //create new Intent
    Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);

    fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO);  // create a file to save the video
    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);  // set the image file name

    intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); // set the video image quality to high

    // start the Video Capture Intent
    startActivityForResult(intent, CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE);
}

Intent中可以设置几种选项:
MediaStore.EXTRA_OUTPUT ——–它和上文描述的相同,表示文件的存储位置

MediaStore.EXTRA_VIDEO_QUALITY——-这个用来设置摄像的质量 0 为低质量,1为高质量
MediaStore.EXTRA_DURATION_LIMIT—-录制的时长 单位为秒
MediaStore.EXTRA_SIZE_LIMIT——–文件的大小,单位为byte

接收示例:

private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
private static final int CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE = 200;

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            // Image captured and saved to fileUri specified in the Intent
            Toast.makeText(this, "Image saved to:\n" +
                     data.getData(), Toast.LENGTH_LONG).show();
        } else if (resultCode == RESULT_CANCELED) {
            // User cancelled the image capture
        } else {
            // Image capture failed, advise user
        }
    }

    if (requestCode == CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            // Video captured and saved to fileUri specified in the Intent
            Toast.makeText(this, "Video saved to:\n" +
                     data.getData(), Toast.LENGTH_LONG).show();
        } else if (resultCode == RESULT_CANCELED) {
            // User cancelled the video capture
        } else {
            // Video capture failed, advise user
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值