android things 调用摄像头一直错误

idea_however 2017-11-01 04:19:21
android things 调用摄像头拍照 拍照一直失败 没有奔溃,只有log错误
代码:

private static final SparseIntArray ORIENTATIONS = new SparseIntArray();

///为了使照片竖直显示
static {
ORIENTATIONS.append(Surface.ROTATION_0, 90);
ORIENTATIONS.append(Surface.ROTATION_90, 0);
ORIENTATIONS.append(Surface.ROTATION_180, 270);
ORIENTATIONS.append(Surface.ROTATION_270, 180);
}

private CameraManager mCameraManager;//摄像头管理器
private Handler childHandler;
private String mCameraID;//摄像头Id 0 为后 1 为前
private ImageReader mImageReader;
private CameraCaptureSession mCameraCaptureSession;
private CameraDevice mCameraDevice;
private ImageView imageView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Window w = getWindow();
w.setFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
setContentView(R.layout.activity_main);

findViewById(R.id.take).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openCamera();
}
});
imageView = (ImageView) findViewById(R.id.iv_show_camera2_activity);
}

private void openCamera() {
HandlerThread handlerThread = new HandlerThread("Camera2");
handlerThread.start();
childHandler = new Handler(handlerThread.getLooper());

mImageReader = ImageReader.newInstance(1080, 1920, ImageFormat.JPEG, 1);
mImageReader.setOnImageAvailableListener(new ImageReader.OnImageAvailableListener() { //可以在这里处理拍照得到的临时照片 例如,写入本地
@Override
public void onImageAvailable(ImageReader reader) {
mCameraDevice.close();
mCameraDevice = null;
// 拿到拍照照片数据
Image image = reader.acquireNextImage();
ByteBuffer buffer = image.getPlanes()[0].getBuffer();
byte[] bytes = new byte[buffer.remaining()];
buffer.get(bytes);//由缓冲区存入字节数组
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
bitmap = Bitmap.createScaledBitmap(bitmap, 30, 30, true);
if (bitmap != null) {
imageView.setImageBitmap(bitmap);
}
}
}, childHandler);

if (mCameraManager == null)
mCameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);

String cameraIds[] = {};
try {
cameraIds = mCameraManager.getCameraIdList();
} catch (CameraAccessException e) {
Log.e("tsb", "Cam access exception getting IDs", e);
}
if (cameraIds.length < 1) {
Log.e("tsb", "No cameras found");
return;
}
mCameraID = cameraIds[0];
try {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
MyUtil.setToast(MainActivity.this, "没有照相机权限", false);
return;
}
mCameraManager.openCamera(mCameraID, stateCallback, null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}

private CameraDevice.StateCallback stateCallback = new CameraDevice.StateCallback() {
@Override
public void onOpened(@NonNull CameraDevice camera) {
//打开摄像头
mCameraDevice = camera;
//拍照
takePicture();
}

@Override
public void onDisconnected(@NonNull CameraDevice camera) {
//关闭摄像头
if (null != mCameraDevice) {
mCameraDevice.close();
mImageReader.close();
}
mCameraDevice = null;
}

@Override
public void onError(@NonNull CameraDevice camera, int error) {
mCameraDevice = null;
//有错误
MyUtil.setToast(MainActivity.this, "摄像头开启失败", false);
}
};

private void takePicture(){
try {
final CaptureRequest.Builder builder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
builder.addTarget(mImageReader.getSurface());
// 自动对焦
builder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
// 打开闪光灯
builder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);
int rotation = getWindowManager().getDefaultDisplay().getRotation();
// 根据设备方向计算设置照片的方向
builder.set(CaptureRequest.JPEG_ORIENTATION, ORIENTATIONS.get(rotation));
mCameraDevice.createCaptureSession(Arrays.asList(mImageReader.getSurface()), new CameraCaptureSession.StateCallback() {
@Override
public void onConfigured(@NonNull CameraCaptureSession session) {
if (mCameraDevice == null)
return;
try {
CaptureRequest mCaptureRequest = builder.build();
session.capture(mCaptureRequest, null, childHandler);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}

@Override
public void onConfigureFailed(@NonNull CameraCaptureSession session) {
MyUtil.setToast(MainActivity.this,"配置错误",false);
}
},childHandler);

} catch (CameraAccessException e) {
e.printStackTrace();
}
}


下面是log错误

11-01 08:15:06.841 412-470/system_process E/SoundPool: error loading /system/media/audio/ui/Effect_Tick.ogg
11-01 08:15:06.841 412-470/system_process E/SoundPool: error loading /system/media/audio/ui/Effect_Tick.ogg
11-01 08:15:06.842 412-470/system_process E/SoundPool: error loading /system/media/audio/ui/Effect_Tick.ogg
11-01 08:15:06.842 412-470/system_process E/SoundPool: error loading /system/media/audio/ui/Effect_Tick.ogg
11-01 08:15:06.843 412-470/system_process E/SoundPool: error loading /system/media/audio/ui/Effect_Tick.ogg
11-01 08:15:06.843 412-470/system_process E/SoundPool: error loading /system/media/audio/ui/KeypressStandard.ogg
11-01 08:15:06.843 412-470/system_process E/SoundPool: error loading /system/media/audio/ui/KeypressSpacebar.ogg
11-01 08:15:06.844 412-470/system_process E/SoundPool: error loading /system/media/audio/ui/KeypressDelete.ogg
11-01 08:15:06.844 412-470/system_process E/SoundPool: error loading /system/media/audio/ui/KeypressReturn.ogg
11-01 08:15:06.845 412-470/system_process E/SoundPool: error loading /system/media/audio/ui/KeypressInvalid.ogg
11-01 08:15:06.926 154-15876/? E/V4L2CameraHAL: virtual bool v4l2_camera_hal::V4L2Camera::isValidRequestSettings(const android::CameraMetadata &):445: Invalid request settings.
11-01 08:15:06.926 154-15876/? E/Camera: int default_camera_hal::Camera::processCaptureRequest(camera3_capture_request_t *):0: Invalid request settings.
11-01 08:15:06.927 154-15876/? E/Camera3-Device: Camera 0: threadLoop: RequestThread: Unable to submit capture request 0 to HAL device: Invalid argument (-22)


请大神指点
...全文
269 2 打赏 收藏 举报
写回复
2 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
idea_however 2017-11-02
引用 1 楼 YXTS122 的回复:
把你的这个项目发到我邮箱1444089871@qq.com,我看看。。。
已解决,参数设置错误,设置了闪光灯
  • 打赏
  • 举报
回复
YXTS122 2017-11-02
把你的这个项目发到我邮箱1444089871@qq.com,我看看。。。
  • 打赏
  • 举报
回复
发帖
Android

7.9w+

社区成员

移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
帖子事件
创建了帖子
2017-11-01 04:19
社区公告
暂无公告