摄像头权限
if (ContextCompat.checkSelfPermission(mContext, Manifest.permission.WRITE_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED) {ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.CAMERA},1);}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions,int[] grantResults) {if (requestCode ==1) {if (grantResults.length >0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {}else {Toast.makeText(this,"CAMERA PERMISSION DENIED", Toast.LENGTH_SHORT).show();}return;}super.onRequestPermissionsResult(requestCode, permissions, grantResults);}
摄像头初始化
protected void initCarema() {
try {
int number = Camera.getNumberOfCameras();
if (number ==2) {
cameraId =1;
}
mCamera = Camera.open(cameraId);//可以根据ID使用不同的摄像头
mCamera.setPreviewTexture(mSurface);
}catch (Exception e) {
e.printStackTrace();
}
if (mCamera !=null) {
setCameraDisplayOrientation(mActivity,cameraId,mCamera);
mCamera.setPreviewCallback(this);
Camera.Parameters parameters =mCamera.getParameters();
// parameters.setPreviewFrameRate(3);//设置每秒3帧,没有效果
// List previewSizes = parameters.getSupportedPreviewSizes();//获得相机预览所支持的大小。
// Camera.Size size1 = previewSizes.get(6);//default 2,4
// parameters.setPreviewSize(size1.width, size1.height);
// parameters.setPreviewSize(600, 800);
parameters.setPreviewSize(getResources().getDimensionPixelSize(R.dimen.camera_w),
getResources().getDimensionPixelSize(R.dimen.camera_h));
mCamera.setParameters(parameters);
mCamera.startPreview();
}
}
预览
textureView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface,int width,int height) {
mSurface = surface;
initCarema();
startScanAnim();
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface,int width,int height) {
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
return false;
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
}
});
}