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;
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.
- textureNormal and textureWide are my textureViews for the preview of the camera
- mPhysicalCameraIdNormal and mPhysicalCameraIdWide are my phyiscalCameraIds
- 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
add a comment |
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.
- textureNormal and textureWide are my textureViews for the preview of the camera
- mPhysicalCameraIdNormal and mPhysicalCameraIdWide are my phyiscalCameraIds
- 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
add a comment |
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.
- textureNormal and textureWide are my textureViews for the preview of the camera
- mPhysicalCameraIdNormal and mPhysicalCameraIdWide are my phyiscalCameraIds
- 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
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.
- textureNormal and textureWide are my textureViews for the preview of the camera
- mPhysicalCameraIdNormal and mPhysicalCameraIdWide are my phyiscalCameraIds
- 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
android android-camera2 multi-camera-api
asked Mar 8 at 19:03
MarcMarc
32
32
add a comment |
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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