前几日在github发现一个很强大的开源框架CameraKit,学习了一下,顺便写了demo(下载地址在下边),还有很多不足之处,还望大家指正。
先大致说下这个框架的用法吧,我用的是拍照和录像的那个版本,使用方法:
1、在build.gradle引入
compile 'com.wonderkiln:camerakit:0.13.2'
2、xml中
<com.wonderkiln.camerakit.CameraView
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" />
xml里边,有一些属性可以自己设置,也可以在代码中手动设置,
比如camerakit:ckFacing="back",手动设置cameraView.setFacing(CameraKit.Constants.FACING_BACK);后置摄像头
再比如camerakit:ckFlash="off",手动设置为cameraView.setFlash(CameraKit.Constants.FLASH_OFF);闪光灯
其他各位想了解的话可以移步去开源框架CameraKit学习一下。
3、activity里边
@Override
protected void onResume() {
super.onResume();
cameraView.start();
}
@Override
protected void onPause() {
cameraView.stop();
super.onPause();
}
4、使用方法
camera.captureImage();//拍照
camera.cameraView.captureVideo();//开始录像
cameraView.stopVideo();//结束录像
//拍照及录像结束回调
cameraView.addCameraKitListener(new CameraKitEventListener() {
@Override
public void onEvent(CameraKitEvent cameraKitEvent) {
}
@Override
public void onError(CameraKitError cameraKitError) {
}
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onImage(CameraKitImage cameraKitImage) {
//cameraKitImage.getJpeg();
//cameraKitImage.getBitmap();
}
@Override
public void onVideo(CameraKitVideo cameraKitVideo) {
//cameraKitImage.getVideoFile();
}
});
//其他方法
// cameraView.getPreviewSize() 获取预览尺寸
//cameraView.isFacingFront() 是否前置摄像头
//getFlash() 获取闪光灯设置类型
//cameraView.setCropOutput(true);设置输出剪裁后图片
//cameraView.setJpegQuality(100);设置图片质量
//cameraView.setVideoQuality(CameraKit.Constants.VIDEO_QUALITY_1080P);设置视频质量
//cameraView.setVideoBitrate(CameraKit.Constants.VIDEO_QUALITY_480P);设置视频比特率
......
好了,用法暂时讲到这里了,下面给大家看下我的demo,还有很多不足之处,还望大家给与批评指正,一起进步,上图:
关键代码
private CameraView cameraView;
private ImageView DO_FLASH;
private ImageView DO_REVARSE;
private CaptureLayout capture;
private VideoView video;
private ImageView img;
private RelativeLayout preview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
cameraView = (CameraView) findViewById(R.id.camera);
DO_FLASH = (ImageView) findViewById(R.id.DO_FLASH);
DO_REVARSE = (ImageView) findViewById(R.id.DO_REVARSE);
preview = (RelativeLayout) findViewById(R.id.rl_preview);
capture = (CaptureLayout) findViewById(R.id.capture_layout);
capture.setCaptureLisenter(this);//拍照及录像事件回调
capture.setTypeLisenter(this);//取消及确认事件回调
capture.setDuration(15 * 1000);//设置录制视频最长时间
capture.setMinDuration(1 * 1000);//设置录制视频最短时间
capture.setIconSrc(R.drawable.ic_back, R.drawable.ic_photo);
video = (VideoView) findViewById(R.id.video);
img = (ImageView) findViewById(R.id.img);
video.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public vo