Android 9:大约同时从物理镜头保存两个图像

时间:2019-03-08 19:03:13

标签: android android-camera2 multi-camera-api

我目前正在开发一个应用程序,该应用程序可以显示来自智能手机的逻辑相机的所有物理镜头。 我现在的问题是,当用户按下“捕获”按钮时,我想以最佳情况一次保存每个物理镜头的图像。 目前,我只得到一张照片,这是第一张或“主”物理镜头产生的。 这是我的代码,用于显示2个物理镜头(效果很好),也是我尝试保存图像的尝试。

  1. textureNormal和textureWide是我用于相机预览的textureViews
  2. mPhysicalCameraIdNormal和mPhysicalCameraIdWide是我的phyiscalCameraIds
  3. 当前,同一图像由ImageReader readerNormal和readerWide保存两次。

private void createCameraPreview() {

try { List<OutputConfiguration> outputConfigurations; SurfaceTexture textureNormal = viewNormal.getSurfaceTexture(); SurfaceTexture textureWide = viewWide.getSurfaceTexture(); assert textureNormal != null && textureWide != null; textureNormal.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight()); textureWide.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight()); Surface surfaceNormal = new Surface(textureNormal); Surface surfaceWide = new Surface(textureWide); captureRequestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW); captureRequestBuilder.addTarget(surfaceWide); captureRequestBuilder.addTarget(surfaceNormal); OutputConfiguration outputConfigurationPhysicalNormal = new OutputConfiguration(surfaceNormal); outputConfigurationPhysicalNormal.setPhysicalCameraId(mPhysicalCameraIdNormal); OutputConfiguration outputConfigurationPhysicalWide = new OutputConfiguration(surfaceWide); outputConfigurationPhysicalWide.setPhysicalCameraId(mPhysicalCameraIdWide); outputConfigurations = new LinkedList<>(); outputConfigurations.add(outputConfigurationPhysicalWide); outputConfigurations.add(outputConfigurationPhysicalNormal); SessionConfiguration sessionConfiguration = new SessionConfiguration( SessionConfiguration.SESSION_REGULAR, outputConfigurations, AsyncTask.THREAD_POOL_EXECUTOR, new CameraCaptureSession.StateCallback() { @Override public void onConfigured(@NonNull CameraCaptureSession session) { if (mCameraDevice == null) return; cameraCaptureSessions = session; updatePreview(); } @Override public void onConfigureFailed(@NonNull CameraCaptureSession session) { Log.e("---------------", "Failed to configure CameraCaptureSession"); } }); mCameraDevice.createCaptureSession(sessionConfiguration); } catch (CameraAccessException e) { e.printStackTrace(); } } private void updatePreview() { if(mCameraDevice == null) { Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show(); } else { captureRequestBuilder.set(CaptureRequest.CONTROL_MODE, CaptureRequest.CONTROL_MODE_AUTO); try { cameraCaptureSessions.setRepeatingRequest(captureRequestBuilder.build(), null, mBackgroundHandler); } catch (CameraAccessException e) { e.printStackTrace(); } } } private void takeImage() { if(mCameraDevice == null) return; try{ CameraCharacteristics characteristics = mCameraManager.getCameraCharacteristics(mCameraId); Size[] jpegSizes = null; if(characteristics != null) jpegSizes = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP) .getOutputSizes(ImageFormat.JPEG); //Capture image with custom size int width = 640; int height = 480; if(jpegSizes != null && jpegSizes.length > 0) { width = jpegSizes[0].getWidth(); height = jpegSizes[0].getHeight(); } final ImageReader readerNormal = ImageReader.newInstance(width, height, ImageFormat.JPEG, 2); final ImageReader readerWide = ImageReader.newInstance(width, height, ImageFormat.JPEG, 2); List<Surface> outputSurface = new ArrayList<>(); outputSurface.add(readerWide.getSurface()); outputSurface.add(readerNormal.getSurface()); final CaptureRequest.Builder captureBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE); captureBuilder.addTarget(readerWide.getSurface()); captureBuilder.addTarget(readerNormal.getSurface()); captureBuilder.set(CaptureRequest.CONTROL_MODE, CaptureRequest.CONTROL_MODE_AUTO); fileNormal = new File(Environment.getExternalStorageDirectory()+"/"+UUID.randomUUID().toString()+"erstes.jpg"); fileWide = new File(Environment.getExternalStorageDirectory()+"/"+UUID.randomUUID().toString()+"zweites.jpg"); ImageReader.OnImageAvailableListener readerListener = new ImageReader.OnImageAvailableListener() { @Override public void onImageAvailable(ImageReader imageReader) { Image image = null; try{ image = imageReader.acquireNextImage(); ByteBuffer buffer = image.getPlanes()[0].getBuffer(); byte[] bytes = new byte[buffer.capacity()]; buffer.get(bytes); save(bytes); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { { if(image != null) image.close(); } } } private synchronized void save(byte[] bytes) throws IOException { OutputStream outputStream = null; try{ counter++; if ((counter % 2) == 0) { outputStream = new FileOutputStream(fileNormal); outputStream.write(bytes); } else { outputStream = new FileOutputStream(fileWide); outputStream.write(bytes); } }finally { if(outputStream != null) outputStream.close(); } } }; readerNormal.setOnImageAvailableListener(readerListener, mBackgroundHandler); readerWide.setOnImageAvailableListener(readerListener, mBackgroundHandler); final CameraCaptureSession.CaptureCallback captureListener = new CameraCaptureSession.CaptureCallback() { @Override public void onCaptureCompleted(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, @NonNull TotalCaptureResult result) { super.onCaptureCompleted(session, request, result); createCameraPreview(); } }; mCameraDevice.createCaptureSession(outputSurface, new CameraCaptureSession.StateCallback() { @Override public void onConfigured(@NonNull CameraCaptureSession cameraCaptureSession) { try{ cameraCaptureSession.capture(captureBuilder.build(), captureListener, mBackgroundHandler); } catch (CameraAccessException e) { e.printStackTrace(); } } @Override public void onConfigureFailed(@NonNull CameraCaptureSession cameraCaptureSession) { } },mBackgroundHandler); } catch (CameraAccessException e) { e.printStackTrace(); } }

谢谢!

0 个答案:

没有答案