功能:图片与视频的拍摄和本地选择

移动开发 简书

前言:

本文只是介绍如何使用,仅仅是介绍一下如何实现图片与视频的拍摄以及它们的本地选择。

感谢作者:陈嘉桐 对CameraView的开源

感谢作者:LuckSiege 对PictureSelector的开源

所有涉及到的权限为:

功能一:拍照与摄像

该功能需要调用手机摄像头进行拍照或摄像。

步骤一: 添加依赖

// 小视频 拍照库

compile ‘cjt.library.wheel:camera:1.1.9’

如果添加依赖失败 则在根目录project gradle中添加如下代码:

allprojects {

repositories {

jcenter()

maven {

url ‘ https://dl.bintray.com/cjt/maven’

}

}

}

步骤二:拍摄界面的XML布局

<com.cjt2325.cameralibrary.JCameraView

android:id=”@+id/jcameraview”

android:layout_width=”match_parent”

android:layout_height=”match_parent”

app:duration_max=”15000″//最长拍摄秒数

app:iconLeft=”@drawable/ic_back”

app:iconMargin=”20dp”

app:iconSize=”30dp”

app:iconSrc=”@drawable/ic_camera” />

步骤三:拍摄页面的java代码

public class CameraActivity extends AppCompatActivity {
    public static final int RESULT_CODE_RETURN_PHOTO = 101;
    public static final int RESULT_CODE_RETURN_VIDEO = 102;
    public static final int RESULT_CODE_PERMISS_REJECT = 103;
    private JCameraView jCameraView;
    private boolean onlyPhotograph;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        setContentView(R.layout.activity_camera);
        //根据需求是否添加以下代码
        Intent intent = getIntent();
        if (intent != null){
            onlyPhotograph = intent.getBooleanExtra("onlyPhotograph", false);
        }
        jCameraView = (JCameraView) findViewById(R.id.jcameraview);
        //设置视频保存路径
        jCameraView.setSaveVideoPath(Environment.getExternalStorageDirectory().getPath() + File.separator + "JCamera");
        //是否只允许拍照
        if (onlyPhotograph){
            jCameraView.setTip("轻触拍照");
            jCameraView.setFeatures(JCameraView.BUTTON_STATE_ONLY_CAPTURE);//只给拍照
        }else {
            jCameraView.setTip("轻触拍照,长按摄像");
            jCameraView.setFeatures(JCameraView.BUTTON_STATE_BOTH);//同时拍照和摄像
        }
        jCameraView.setMediaQuality(JCameraView.MEDIA_QUALITY_MIDDLE);//设置视频质量
        jCameraView.setErrorLisenter(new ErrorListener() {
            @Override
            public void onError() {
            //错误监听,失败时回调
                Log.i("CJT", "camera error");
                Intent intent = new Intent();
                setResult(RESULT_CODE_PERMISS_REJECT, intent);
                finish();
            }

            @Override
            public void AudioPermissionError() {
                Toast.makeText(CameraActivity.this, "请检查是否开启录音权限", Toast.LENGTH_SHORT).show();
            }
        });

        //JCameraView监听
        jCameraView.setJCameraLisenter(new JCameraListener() {
            @Override
            public void captureSuccess(Bitmap bitmap) {
                //获取拍照图片bitmap
                Log.i("JCameraView", "bitmap = " + bitmap.getWidth());
                String path = FileUtil.saveBitmap("JCamera", bitmap);
                Intent intent = new Intent();
                intent.putExtra("path", path);
                setResult(RESULT_CODE_RETURN_PHOTO, intent);
                finish();
            }

            @Override
            public void recordSuccess(String url, Bitmap firstFrame) {//视频路径,首帧图
                //获取视频首帧图并转成路径
                String path = FileUtil.saveBitmap("JCamera", firstFrame);//FileUtil是本库自带的
                Log.i("CJT", "url = " + url + ", Bitmap = " + path);
                Intent intent = new Intent();
                intent.putExtra("path", path);
                intent.putExtra("videoUrl", url);
                setResult(RESULT_CODE_RETURN_VIDEO, intent);
                finish();
            }
        });
       
        jCameraView.setLeftClickListener(new ClickListener() {
            @Override
            public void onClick() {
                CameraActivity.this.finish();
            }
        });
        jCameraView.setRightClickListener(new ClickListener() {
            @Override
            public void onClick() {
                Toast.makeText(CameraActivity.this,"Right",Toast.LENGTH_SHORT).show();
            }
        });

        Log.i("CJT", DeviceUtil.getDeviceModel());
    }

    //JCameraView生命周期
    @Override
    protected void onStart() {
        super.onStart();
        //全屏显示
        if (Build.VERSION.SDK_INT >= 19) {
            View decorView = getWindow().getDecorView();
            decorView.setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
        } else {
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_FULLSCREEN;
            decorView.setSystemUiVisibility(option);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        jCameraView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        jCameraView.onPause();
    }  
}

步骤四:在Activity/Fragment中调用拍摄页面,这里以Activity为例

//跳转到拍摄页面
private void toCameraActivity() {
    // 6.0 以上权限动态处理
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (!(ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED &&
                ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED &&
                ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED)) {
            //不具有获取权限,需要进行权限申请
            ActivityCompat.requestPermissions(CatCircleEditActivity.this, new String[]{
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.RECORD_AUDIO,
                    Manifest.permission.CAMERA}, Constants.REQUEST_CAMERA);//请自定义最后的参数:常量
            return;
        }
    }
    //根据业务需要添加
    if (numPictureTotal > 0) {//如果当前有图片,打开相机不允许长按录像
        onlyPhotograph = true;
    }else {
        onlyPhotograph = false;
    }
    // 跳转到 拍摄界面
    startActivityForResult(new Intent(CatCircleEditActivity.this, CameraActivity.class).putExtra("onlyPhotograph", onlyPhotograph), Constants.REQUESTCODE_CAPTURE_CAMEIA);//请自定义最后一个参数:常量
}                                                        

//处理拍照/摄像的返回值
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (data == null) {
        return;
    }
    if (requestCode == Constants.REQUESTCODE_CAPTURE_CAMEIA) {
        if(resultCode == RESULT_CODE_RETURN_PHOTO){//拍照
            //照片路径
            String photoPath = data.getStringExtra("path");
        }else if(resultCode == RESULT_CODE_RETURN_VIDEO){//摄像
            //视频第一帧图片路径
            String firstVideoPicture = data.getStringExtra("path");
            //视频路径,该路径为已压缩过的视频路径
            String videoPath = data.getStringExtra("videoUrl");
        }
    }
}

//处理权限                                                         
@Override  
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    switch (requestCode){
        case Constants.REQUEST_CAMERA:
            int size = 0;
            if (grantResults.length >= 1) {
                int writeResult = grantResults[0];
                //读写内存权限
                boolean writeGranted = writeResult == PackageManager.PERMISSION_GRANTED;
                if (!writeGranted) {
                    size++;
                }
                //录音权限
                int recordPermissionResult = grantResults[1];
                boolean recordPermissionGranted = recordPermissionResult == PackageManager.PERMISSION_GRANTED;
                if (!recordPermissionGranted) {
                    size++;
                }
                //相机权限
                int cameraPermissionResult = grantResults[2];
                boolean cameraPermissionGranted = cameraPermissionResult == PackageManager.PERMISSION_GRANTED;
                if (!cameraPermissionGranted) {
                    size++;
                }
                if (size == 0) {
                    startActivityForResult(new Intent(CatCircleEditActivity.this, CameraActivity.class), 100);
                } else {
                    Toast.makeText(this, "请到设置-权限管理中开启", Toast.LENGTH_SHORT).show();
                }
            }
            break;
        default:
            break;
    }
}

至此:CameraView 实现拍照和摄像功能就此完成。下面介绍如何实现本地图片和视频的选择。

功能二:本地选择图片和视频

注意:本库中的图片加载使用的是Glide4.+ 如果项目中存在旧版本的Glide可以替换成4.+版本。在具体的使用过程会遇到的哪些错误,请到 https://github.com/LuckSiege/PictureSelector
去看看相关介绍

步骤一:添加依赖

// 图片选择库

compile ‘com.github.LuckSiege.PictureSelector:picture_library:v2.1.7’

并在根目录project gradle中添加如下代码:

allprojects {

repositories {

jcenter()

maven { url ‘https://jitpack.io’ }

}

}

步骤二:在Activity中使用

只举部分常用功能,更多的功能请到上面链接处看项目介绍

mPictureIV.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //场景一:全部选择(图片/视频/gif)
            PictureSelector.create(SsMainActivity.this)
                    .openGallery(PictureMimeType.ofAll()).isGif(true)
                    .maxSelectNum(9).minSelectNum(1)//选择数量1~9
                    .previewImage(true)//图片预览
                    .previewVideo(true)//视频预览
                    .forResult(PictureConfig.CHOOSE_REQUEST);

            // 场景二:选择图片(图片/gif)
            PictureSelector.create(CatCircleEditActivity.this)
                    .openGallery(PictureMimeType.ofImage()).isGif(true).isCamera(false).previewImage(false)
                    .maxSelectNum(9).minSelectNum(1)
                    .enableCrop()//是否裁剪
                    .compress()//是否压缩
                    .forResult( PictureConfig.CHOOSE_REQUEST);
            
            //场景三:选择视频(只有视频)
            PictureSelector.create(CatCircleEditActivity.this)
                    .openGallery(PictureMimeType.ofVideo()).isCamera(false).previewVideo(false)
                    .maxSelectNum(1).minSelectNum(1).videoMaxSecond(30).forResult( PictureConfig.CHOOSE_REQUEST);
        }
    });

//处理返回值
 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (data == null) {
        return;
    }
    if (resultCode == RESULT_OK) {
        //返回的图片/gif/视频的路径组成的List集合
        List selectList = new ArrayList();
        selectList = PictureSelector.obtainMultipleResult(data);
        //!注意,只有图片存在压缩的情况,gif和video不存在压缩

        if (selectList.size() > 0) {
            for (int i = 0; i < selectList.size(); i++) {
                //场景一:getPath()为原图片/gif/视频路径
                Log.i("原路径为", selectList.get(i).getPath());
                    
                //场景二:getCutPath()为裁剪后的路径
                if (selectList.get(i).isCut())) {//裁剪过
                    Log.i("裁剪后的路径为", selectList.get(i).getPath());
                }

                //场景三:getCompressPath()为压缩后的路径
                if (selectList.get(i).isCompressed())) {//压缩过
                     Log.i("压缩后的路径为", selectList.get(i).getCompressPath())                       }
                }
            } 
        } 
    }
 }

结束。

简书稿源:简书 (源链) | 关于 | 阅读提示

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 移动开发 » 功能:图片与视频的拍摄和本地选择

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录