Camera

2016-05-25  本文已影响0人  BackToLife

注意事项

基础

Android平台通过 android.hardware.camera2 API或者 camera Intent来支持拍照开发,相关类如下:

Manifest声明

相机权限

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

相机特征

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

相机特征用来表明你不想把应用安装在没有相机的设备上

//TODO:(表示这个暂时不懂)
如果你为了正确的操作而使用相机或者相机特征,但是你并不需要它,你需要声明android:required属性,并且设置为false

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

存储权限--如果你需要保存照片或视频到SD卡上

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

音频录制权限

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

位置权限

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

使用现有相机软件

使用Intent来调用现有APP是一个快速方式,不需要额外代码。

  1. 集成Camera Intent
  1. 发送意图--startActivityForResult()来执行
  2. 接收结果-- 使用onActivityResult()来接收结果

拍照

一个拍照Intent可以携带以下信息

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

摄像

Intent可以携带以下信息:

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结果

下面的例子展示如何使用onActivityResult()接收相机返回的结果

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

专门找了一个4.2.2版本的酷派手机,表示并不是所有功能都如上所说,有些API无效,有些会引起BUG,返回的Intent里也读不到该有的数据。

建立相机APP

由于官方建议使用 android.hardware.camera2包下的API,所以先来看看这个包的介绍

android.hardware.camera2提供相机设备的操作接口,取代了Camera类
这个包对相机设备建模成一个管道,这个管道用来接收拍照请求,为每个请求抓取单独图片,并输出一个数据包,外加这个图片的缓存。请求在队列中进行,并且多个请求可以立刻完成。因为相机设备是一个有多阶管道,所以需要多次请求来保持帧率(多数android设备上)
通过CameraManager对象来管理、查询、打开可用的相机设备
CameraDevices提供一组静态信息,来描述硬件设备和可用的设置和参数。这些信息通过CameraCharacteristics对象提供,对象由getCameraCharacteristics(String)获得。

上一篇 下一篇

猜你喜欢

热点阅读