标签:number aci void creat camera width height oid 宽高
ActivityCompat.requestPermissions(SomeActivity.this,new String[]{Manifest.permission.CAMERA},1);
<SurfaceView
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:id="@+id/camera_surfaceview"
app:layout_constraintTop_toTopOf="parent"
android:layout_width="0.1dp"
android:layout_height="0.1dp"
/>
SurfaceViewHolder myHolder = surfaceViewHolder.getHolder();
myHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
Camera camera = getCamera();
/**
* 获取前置摄像头
* @return
*/
public Camera getCamera() {
camera = null;
Camera.CameraInfo info = new Camera.CameraInfo();
int cnt = Camera.getNumberOfCameras();
for (int i = 0;i<cnt;i++) {
Camera.getCameraInfo(i,info);
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
try {
camera = Camera.open(i);
} catch (RuntimeException e) {
e.printStackTrace();
}
}
}
return camera;
}
if (camera != null) {
try {
camera.setPreviewDisplay(myHolder);
camera.startPreview();
camera.takePicture(null, null, jpeg);
camera.setErrorCallback(callback);
} catch(IOException e) {
e.printStackTrace();
camera.stopPreview();
camera.release();
camera=null;
}
} else {
// camera为空
}
//onCreate()函数外部
final Camera.PictureCallback jpeg = (data, camera) -> {
//根据拍照所得数据创建位图
Bitmap bitmap = BitmapFactory.decodeByteArray(data,0,data.length);
//执行照片存储等相关操作,注意bitmap的回收
//...
camera.stopPreview();
};
Camera.ErrorCallback callback = new Camera.ErrorCallback() {
@Override
public void onError(int error, Camera camera) {
switch (error) {
case Camera.CAMERA_ERROR_SERVER_DIED:
Log.e("error", "Camera.CAMERA_ERROR_SERVER_DIED");
//这里重新初始化Camera即可
break;
case Camera.CAMERA_ERROR_UNKNOWN:
Log.e("error", "Camera.CAMERA_ERROR_UNKNOWN");
break;
}
}
};
@Override
public void onDestroy() {
super.onDestroy();
if (camera != null) {
camera.release();
camera = null;
}
}
标签:number aci void creat camera width height oid 宽高
原文地址:https://www.cnblogs.com/Ricardoldc/p/10961646.html