This repository has been archived by the owner on Dec 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1k
Added preview callback #123
Open
Sumsar
wants to merge
1
commit into
google:master
Choose a base branch
from
Sumsar:rg_preview_callback
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,7 @@ | |
import android.media.Image; | ||
import android.media.ImageReader; | ||
import android.support.annotation.NonNull; | ||
import android.support.annotation.Nullable; | ||
import android.util.Log; | ||
import android.util.SparseIntArray; | ||
import android.view.Surface; | ||
|
@@ -156,19 +157,26 @@ public void onReady() { | |
|
||
@Override | ||
public void onImageAvailable(ImageReader reader) { | ||
try (Image image = reader.acquireNextImage()) { | ||
Image.Plane[] planes = image.getPlanes(); | ||
if (planes.length > 0) { | ||
ByteBuffer buffer = planes[0].getBuffer(); | ||
byte[] data = new byte[buffer.remaining()]; | ||
buffer.get(data); | ||
mCallback.onPictureTaken(data); | ||
} | ||
byte[] data = getByteDataFromImageReader(reader); | ||
if (data != null) { | ||
mCallback.onPictureTaken(data); | ||
} | ||
} | ||
|
||
}; | ||
|
||
private final ImageReader.OnImageAvailableListener mOnPreviewImageAvailableListener | ||
= new ImageReader.OnImageAvailableListener() { | ||
|
||
@Override | ||
public void onImageAvailable(ImageReader reader) { | ||
byte[] data = getByteDataFromImageReader(reader); | ||
if (data != null) { | ||
mCallback.onPreviewFrame(data, reader.getImageFormat(), | ||
reader.getWidth(), | ||
reader.getHeight()); | ||
} | ||
} | ||
}; | ||
|
||
private String mCameraId; | ||
|
||
|
@@ -182,6 +190,8 @@ public void onImageAvailable(ImageReader reader) { | |
|
||
private ImageReader mImageReader; | ||
|
||
private ImageReader mCallbackImageDataReader; | ||
|
||
private final SizeMap mPreviewSizes = new SizeMap(); | ||
|
||
private final SizeMap mPictureSizes = new SizeMap(); | ||
|
@@ -232,6 +242,11 @@ void stop() { | |
mImageReader.close(); | ||
mImageReader = null; | ||
} | ||
|
||
if (mCallbackImageDataReader != null) { | ||
mCallbackImageDataReader.close(); | ||
mCallbackImageDataReader = null; | ||
} | ||
} | ||
|
||
@Override | ||
|
@@ -435,11 +450,49 @@ protected void collectPictureSizes(SizeMap sizes, StreamConfigurationMap map) { | |
|
||
private void prepareImageReader() { | ||
Size largest = mPictureSizes.sizes(mAspectRatio).last(); | ||
mImageReader = ImageReader.newInstance(largest.getWidth(), largest.getHeight(), | ||
initImageReader(largest.getWidth(), largest.getHeight()); | ||
|
||
Size size = chooseOptimalSize(); | ||
initCallbackImageDataReader(size.getWidth(), size.getHeight()); | ||
} | ||
|
||
private void initImageReader(int width, int height){ | ||
mImageReader = ImageReader.newInstance(width, height, | ||
ImageFormat.JPEG, /* maxImages */ 2); | ||
mImageReader.setOnImageAvailableListener(mOnImageAvailableListener, null); | ||
} | ||
|
||
private void initCallbackImageDataReader(int width, int height) { | ||
|
||
if (mCallbackImageDataReader != null) { | ||
mCallbackImageDataReader.close(); | ||
} | ||
|
||
mCallbackImageDataReader = ImageReader.newInstance(width, height, | ||
ImageFormat.NV21, /* maxImages */ 1); | ||
mCallbackImageDataReader.setOnImageAvailableListener(mOnPreviewImageAvailableListener, null); | ||
} | ||
|
||
/** | ||
* Extract the byte data from the ImageReader | ||
* @param reader | ||
* @return byte array or null if failed | ||
*/ | ||
@Nullable | ||
private byte[] getByteDataFromImageReader(@NonNull ImageReader reader) { | ||
byte[] data = null; | ||
try (Image image = reader.acquireNextImage()) { | ||
Image.Plane[] planes = image.getPlanes(); | ||
if (planes.length > 0) { | ||
ByteBuffer buffer = planes[0].getBuffer(); | ||
data = new byte[buffer.remaining()]; | ||
buffer.get(data); | ||
} | ||
} | ||
return data; | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Some image formats have multiple planes, including YUV420_888 (the default for Camera2). All planes need to be included in the output byte array. |
||
|
||
/** | ||
* <p>Starts opening a camera device.</p> | ||
* <p>The result will be processed in {@link #mCameraDeviceCallback}.</p> | ||
|
@@ -463,11 +516,14 @@ void startCaptureSession() { | |
} | ||
Size previewSize = chooseOptimalSize(); | ||
mPreview.setBufferSize(previewSize.getWidth(), previewSize.getHeight()); | ||
|
||
Surface surface = mPreview.getSurface(); | ||
try { | ||
mPreviewRequestBuilder = mCamera.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW); | ||
mPreviewRequestBuilder.addTarget(surface); | ||
mCamera.createCaptureSession(Arrays.asList(surface, mImageReader.getSurface()), | ||
mPreviewRequestBuilder.addTarget(mCallbackImageDataReader.getSurface()); | ||
mCamera.createCaptureSession(Arrays.asList(surface, mImageReader.getSurface(), | ||
mCallbackImageDataReader.getSurface()), | ||
mSessionCallback, null); | ||
} catch (CameraAccessException e) { | ||
throw new RuntimeException("Failed to start camera session"); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, I don't think
NV21
is supported inCamera2
. It should beYUV_420_888
inCamera2
case. It cashed on my N5X withCaused by: java.lang.IllegalArgumentException: NV21 format is not supported
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reference on the above comment