TakePhotoX
基于camerax的拍照邓尤,錄像,二維碼掃描
github源碼:https://github.com/yijiebuyi/TakePhotoX
Demo下載
安裝密碼:123456
功能
- 支持錄像
- 支持前后攝像頭切換
- 支持4:3 16:9 1:1 圖片拍攝
- 支持二維碼掃描識別
- 支持燈光控制
使用
- 1.在project的build.gradle添加如下代碼
allprojects {
repositories {
...
maven { url "https://jitpack.io" }
}
}
- 2.依賴androidx
dependencies {
implementation 'com.github.yijiebuyi:TakePhotoX:v1.2.2'
}
一、拍照基本用法:
- 使用CameraView汞扎,自己實現(xiàn)相機ui
//CameraView對外提供的功能殿漠,詳細見ICamera,IFlashLight
//CameraView使用
private CameraView mCameraView;
//================常用的功能如下=================
//拍照
mCameraView.take();
//對焦
mCameraView.focus(float x, float y, float rawX, float rawY);
//切換前置后置
mCameraView.switchFace();
//相機切換預(yù)覽比例和拍照比例
mCameraView.switchAspect(@ExAspectRatio.ExRatio int ratio);
//================設(shè)置回調(diào)======================
//設(shè)置拍照回調(diào)
mCameraView.setOnCameraListener(OnCameraListener l);
//設(shè)置對焦回調(diào)
mCameraView.setOnFocusListener(OnFocusListener l);
//設(shè)置圖片分析回調(diào)
mCameraView.setOnImgAnalysisListener(OnImgAnalysisListener l);
//設(shè)置前后攝像頭切換回調(diào)
mCameraView.setOnCameraFaceListener(OnCameraFaceListener l);
//設(shè)置相機預(yù)覽view的布局和尺寸變化回調(diào)
mCameraView.setOnPreviewLayoutListener(OnPreviewLayoutListener l);
- 也可以使用CameraFragment佩捞,使用默認的提供的ui效果
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
final CameraFragment cfg = new CameraFragment();
CameraOption option = new CameraOption.Builder(ExAspectRatio.RATIO_16_9)
.faceFront(false) //false:打開后置攝像頭
.build();
Bundle data = new Bundle();
data.putSerializable(CameraFragment.KEY_CAMERA_OPTION, option);
cfg.setArguments(data);
cfg.setOnCameraListener(new OnCameraListener() {
@Override
public void onTaken(Uri uri) {
//返回拍照圖片uri
}
@Override
public void onCancel() {
finish();
}
});
- 直接使用CameraXActivity
startActivityForResult(new Intent(MainActivity.this, CameraXActivity.class), 1000);
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && data != null) {
//返回拍照的圖片地址
Uri uri = data.getData();
}
}
二绞幌、視頻拍攝基本用法:
- 使用VideoXActivity
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && data != null) {
Uri uri = data.getData();
TextView tv = findViewById(R.id.photo_path);
tv.setText(uri.getPath());
}
}
三、二維碼掃描基本用法:
注:需要依賴zxing的包
- 二維碼掃描 直接使用QrCodeFragment
setWrapQrCodeCallback
- 二維碼掃描 使用QRCodeView
mQRCodeView = new QRCodeView(mContext);
mQRCodeView.setOnQrCodeCallback(QrCodeCallback callback);
mQRCodeView.setScannerFrameOption(new ScannerFrameOption.Builder()
.frameMode(ScannerFrameOption.FrameMode.MODE_FRAME_SQUARE)
.frameRatio(0.6f)
.build());
//add mQRCodeView