android快速实现自定义相机

 首先参考或使用到的开源库

https://github.com/gogopop/CameraKit-Android

https://github.com/andyb129/FlipsideCamera

使用方法:

1,首先在module级别build.gradle文件中增加依赖

compile 'com.flurgle:camerakit:0.9.17'

2,增加CameraView到布局文件中

<com.flurgle.camerakit.CameraView
    android:id="@+id/camera"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true" />

3,在你的activity中onResume 和 onPause 进行打开和关闭相机操作

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

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

 

拍摄图片就是调用CameraView.captureImage()方法,代码如下:

camera.setCameraListener(new CameraListener() {
    @Override
    public void onPictureTaken(byte[] picture) {
        super.onPictureTaken(picture);

        // Create a bitmap
        Bitmap result = BitmapFactory.decodeByteArray(picture, 0, picture.length);
    }
});

camera.captureImage();

拍摄视频开始是调用CameraView.startRecordingVideo()方法,结束是调用CameraView.stopRecordingVideo()方法,代码如下:

camera.setCameraListener(new CameraListener() {
    @Override
    public void onVideoTaken(File video) {
        super.onVideoTaken(video);
        // The File parameter is an MP4 file.
    }
});

camera.startRecordingVideo();
camera.postDelayed(new Runnable() {
    @Override
    public void run() {
        camera.stopRecordingVideo();
    }
}, 2500);

其他的一些经常使用到的属性:

<com.flurgle.camerakit.CameraView xmlns:camerakit="http://schemas.android.com/apk/res-auto"
    android:id="@+id/camera"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    camerakit:ckFacing="back"
    camerakit:ckFlash="off"
    camerakit:ckFocus="continuous"
    camerakit:ckMethod="standard"
    camerakit:ckZoom="pinch"
    camerakit:ckPermissions="strict"
    camerakit:ckCropOutput="true"  
    camerakit:ckJpegQuality="100"
    camerakit:ckVideoQuality="480p"
    android:adjustViewBounds="true" />

 

posted @ 2017-06-05 17:05  客舍青  阅读(7525)  评论(0编辑  收藏  举报