Android 9: Saving two images from physical lenses approximately at the same time Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Android - Create a very large ListView based on a SQL CursorHow to attach pdf file from assets in email?Android ksoap2 webs service returning falseLoadmanager onLoadFinished not calledAndroid QuickBlox getFile response issueNanoHTTPD. Cache InputStream to file and continue streamingCan someone show me a simple working implementation of PagerSlidingTabStrip?Session Cookie Not Persistant in Retrofit AndroidView.SurfaceView, why its member, mSurfaceHolder, returns null from getSurface()?Android - Vpnservice DatagramChannel.open() not working

For a new assistant professor in CS, how to build/manage a publication pipeline

Maximum summed powersets with non-adjacent items

How to write this math term? with cases it isn't working

When the Haste spell ends on a creature, do attackers have advantage against that creature?

Using et al. for a last / senior author rather than for a first author

How to deal with a team lead who never gives me credit?

What do you call the main part of a joke?

Why aren't air breathing engines used as small first stages?

How do I find out the mythology and history of my Fortress?

Why wasn't DOSKEY integrated with COMMAND.COM?

What is the longest distance a player character can jump in one leap?

Is there a kind of relay only consumes power when switching?

Is it a good idea to use CNN to classify 1D signal?

What's the meaning of "fortified infraction restraint"?

Most bit efficient text communication method?

How to tell that you are a giant?

Should I use a zero-interest credit card for a large one-time purchase?

How come Sam didn't become Lord of Horn Hill?

What is homebrew?

Why are both D and D# fitting into my E minor key?

How to react to hostile behavior from a senior developer?

Can an alien society believe that their star system is the universe?

Would "destroying" Wurmcoil Engine prevent its tokens from being created?

Crossing US/Canada Border for less than 24 hours



Android 9: Saving two images from physical lenses approximately at the same time



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!Android - Create a very large ListView based on a SQL CursorHow to attach pdf file from assets in email?Android ksoap2 webs service returning falseLoadmanager onLoadFinished not calledAndroid QuickBlox getFile response issueNanoHTTPD. Cache InputStream to file and continue streamingCan someone show me a simple working implementation of PagerSlidingTabStrip?Session Cookie Not Persistant in Retrofit AndroidView.SurfaceView, why its member, mSurfaceHolder, returns null from getSurface()?Android - Vpnservice DatagramChannel.open() not working



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I am currently developing an app, that displays all physical lenses of a logical camera from a smartphone.
My problem now is, when the user presses the button "capture" I want to save the images by each physical lens in best case at once.
Currently, I only get my hands on one picture, that results from the first or the "main" physical lens.
This is my code to display 2 physical lenses (which works fine) and my attempt to save the images.



  1. textureNormal and textureWide are my textureViews for the preview of the camera

  2. mPhysicalCameraIdNormal and mPhysicalCameraIdWide are my phyiscalCameraIds

  3. And currently, the same image gets saved twice by the ImageReader readerNormal and 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();






Thanks in advance!










share|improve this question




























    0















    I am currently developing an app, that displays all physical lenses of a logical camera from a smartphone.
    My problem now is, when the user presses the button "capture" I want to save the images by each physical lens in best case at once.
    Currently, I only get my hands on one picture, that results from the first or the "main" physical lens.
    This is my code to display 2 physical lenses (which works fine) and my attempt to save the images.



    1. textureNormal and textureWide are my textureViews for the preview of the camera

    2. mPhysicalCameraIdNormal and mPhysicalCameraIdWide are my phyiscalCameraIds

    3. And currently, the same image gets saved twice by the ImageReader readerNormal and 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();






    Thanks in advance!










    share|improve this question
























      0












      0








      0








      I am currently developing an app, that displays all physical lenses of a logical camera from a smartphone.
      My problem now is, when the user presses the button "capture" I want to save the images by each physical lens in best case at once.
      Currently, I only get my hands on one picture, that results from the first or the "main" physical lens.
      This is my code to display 2 physical lenses (which works fine) and my attempt to save the images.



      1. textureNormal and textureWide are my textureViews for the preview of the camera

      2. mPhysicalCameraIdNormal and mPhysicalCameraIdWide are my phyiscalCameraIds

      3. And currently, the same image gets saved twice by the ImageReader readerNormal and 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();






      Thanks in advance!










      share|improve this question














      I am currently developing an app, that displays all physical lenses of a logical camera from a smartphone.
      My problem now is, when the user presses the button "capture" I want to save the images by each physical lens in best case at once.
      Currently, I only get my hands on one picture, that results from the first or the "main" physical lens.
      This is my code to display 2 physical lenses (which works fine) and my attempt to save the images.



      1. textureNormal and textureWide are my textureViews for the preview of the camera

      2. mPhysicalCameraIdNormal and mPhysicalCameraIdWide are my phyiscalCameraIds

      3. And currently, the same image gets saved twice by the ImageReader readerNormal and 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();






      Thanks in advance!







      android android-camera2 multi-camera-api






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 8 at 19:03









      MarcMarc

      32




      32






















          0






          active

          oldest

          votes












          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55069440%2fandroid-9-saving-two-images-from-physical-lenses-approximately-at-the-same-time%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55069440%2fandroid-9-saving-two-images-from-physical-lenses-approximately-at-the-same-time%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Save data to MySQL database using ExtJS and PHP [closed]2019 Community Moderator ElectionHow can I prevent SQL injection in PHP?Which MySQL data type to use for storing boolean valuesPHP: Delete an element from an arrayHow do I connect to a MySQL Database in Python?Should I use the datetime or timestamp data type in MySQL?How to get a list of MySQL user accountsHow Do You Parse and Process HTML/XML in PHP?Reference — What does this symbol mean in PHP?How does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?

          Compiling GNU Global with universal-ctags support Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Tags for Emacs: Relationship between etags, ebrowse, cscope, GNU Global and exuberant ctagsVim and Ctags tips and trickscscope or ctags why choose one over the other?scons and ctagsctags cannot open option file “.ctags”Adding tag scopes in universal-ctagsShould I use Universal-ctags?Universal ctags on WindowsHow do I install GNU Global with universal ctags support using Homebrew?Universal ctags with emacsHow to highlight ctags generated by Universal Ctags in Vim?

          Add ONERROR event to image from jsp tldHow to add an image to a JPanel?Saving image from PHP URLHTML img scalingCheck if an image is loaded (no errors) with jQueryHow to force an <img> to take up width, even if the image is not loadedHow do I populate hidden form field with a value set in Spring ControllerStyling Raw elements Generated from JSP tagds with Jquery MobileLimit resizing of images with explicitly set width and height attributeserror TLD use in a jsp fileJsp tld files cannot be resolved