Android 多媒体------相机

一.拍照

1.设置使用相机权限

  为了让用户知道我们的应用需要依赖相机,在Manifest清单文件中添加<uses-feature>标签:

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

  当然我们有必要在使用相机之前通过调用hasSystemFeature(PackageManager.FEATURE_CAMERA)方法来检查设备上是否有相机。如果没有,我们应该禁用和相机相关的功能!

  此外还需要保证我们有使用Camera的权限:

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

2.使用相机应用程序进行拍照

  实现拍照执行Intent对象实现:

static final int REQUEST_IMAGE_CAPTURE = 1;

private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
    }
}

  下面是简单实现调用相机拍照并且将拍照结构显示在ImageView的实现代码:

package com.example.hp.photointent;

import android.content.Intent;
import android.graphics.Bitmap;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    static final int REQUEST_IMAGE_CAPTURE = 1;
    private ImageView mImageView;
    private Button btn_take_photo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }
    void init(){
        mImageView=(ImageView)findViewById(R.id.imageView1);
        btn_take_photo=(Button)findViewById(R.id.btn_take_photo);
        btn_take_photo.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch( view.getId()){
            case R.id.btn_take_photo:
                dispatchTakePictureIntent();
                break;
            default:
                break;

        }

    }
  //Android的相机应用会把拍好的照片编码为缩小的Bitmap,使用extra value的方式添加到返回的Intent当中
  //并传送给onActivityResult(),对应的Key为"data"。 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) { Bundle extras = data.getExtras(); Bitmap imageBitmap = (Bitmap) extras.get("data"); mImageView.setImageBitmap(imageBitmap); } } //调用拍照Intent private void dispatchTakePictureIntent() { Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); if (takePictureIntent.resolveActivity(getPackageManager()) != null) { startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE); } } }

二.录像

  首先跟拍照一样需要获取相机权限,类似的,我们也是使用Intent对象来打开系统内置程序来录制视频。

static final int REQUEST_VIDEO_CAPTURE = 1;

private void dispatchTakeVideoIntent() {
    Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
    if (takeVideoIntent.resolveActivity(getPackageManager()) != null) {
        startActivityForResult(takeVideoIntent, REQUEST_VIDEO_CAPTURE);
    }
}

  Android的相机程序会把指向视频存储地址的URL添加到Intent中,所以我们可以通过返回的Idata来查看视频。

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_VIDEO_CAPTURE && resultCode == RESULT_OK) {
        Uri videoUri = intent.getData();
        mVideoView.setVideoURI(videoUri);
    }
}

  

posted @ 2017-08-21 15:29  sloth_ccc  阅读(197)  评论(0编辑  收藏  举报