Sync sample browser prebuilts for mnc-docs
Synced to //developers/samples/android commit 44c699bf11bae6c9eef1f8c56bd405d547e179e6. Change-Id: I61ceec197e6fa420fc5a6d16e703aa1aab2b0c4e
This commit is contained in:
@@ -28,6 +28,7 @@ import android.content.pm.PackageManager;
|
||||
import android.content.res.Configuration;
|
||||
import android.graphics.ImageFormat;
|
||||
import android.graphics.Matrix;
|
||||
import android.graphics.Point;
|
||||
import android.graphics.RectF;
|
||||
import android.graphics.SurfaceTexture;
|
||||
import android.hardware.camera2.CameraAccessException;
|
||||
@@ -116,6 +117,16 @@ public class Camera2BasicFragment extends Fragment
|
||||
*/
|
||||
private static final int STATE_PICTURE_TAKEN = 4;
|
||||
|
||||
/**
|
||||
* Max preview width that is guaranteed by Camera2 API
|
||||
*/
|
||||
private static final int MAX_PREVIEW_WIDTH = 1920;
|
||||
|
||||
/**
|
||||
* Max preview height that is guaranteed by Camera2 API
|
||||
*/
|
||||
private static final int MAX_PREVIEW_HEIGHT = 1080;
|
||||
|
||||
/**
|
||||
* {@link TextureView.SurfaceTextureListener} handles several lifecycle events on a
|
||||
* {@link TextureView}.
|
||||
@@ -344,31 +355,48 @@ public class Camera2BasicFragment extends Fragment
|
||||
}
|
||||
|
||||
/**
|
||||
* Given {@code choices} of {@code Size}s supported by a camera, chooses the smallest one whose
|
||||
* width and height are at least as large as the respective requested values, and whose aspect
|
||||
* ratio matches with the specified value.
|
||||
* Given {@code choices} of {@code Size}s supported by a camera, choose the smallest one that
|
||||
* is at least as large as the respective texture view size, and that is at most as large as the
|
||||
* respective max size, and whose aspect ratio matches with the specified value. If such size
|
||||
* doesn't exist, choose the largest one that is at most as large as the respective max size,
|
||||
* and whose aspect ratio matches with the specified value.
|
||||
*
|
||||
* @param choices The list of sizes that the camera supports for the intended output class
|
||||
* @param width The minimum desired width
|
||||
* @param height The minimum desired height
|
||||
* @param aspectRatio The aspect ratio
|
||||
* @param choices The list of sizes that the camera supports for the intended output
|
||||
* class
|
||||
* @param textureViewWidth The width of the texture view relative to sensor coordinate
|
||||
* @param textureViewHeight The height of the texture view relative to sensor coordinate
|
||||
* @param maxWidth The maximum width that can be chosen
|
||||
* @param maxHeight The maximum height that can be chosen
|
||||
* @param aspectRatio The aspect ratio
|
||||
* @return The optimal {@code Size}, or an arbitrary one if none were big enough
|
||||
*/
|
||||
private static Size chooseOptimalSize(Size[] choices, int width, int height, Size aspectRatio) {
|
||||
private static Size chooseOptimalSize(Size[] choices, int textureViewWidth,
|
||||
int textureViewHeight, int maxWidth, int maxHeight, Size aspectRatio) {
|
||||
|
||||
// Collect the supported resolutions that are at least as big as the preview Surface
|
||||
List<Size> bigEnough = new ArrayList<>();
|
||||
// Collect the supported resolutions that are smaller than the preview Surface
|
||||
List<Size> notBigEnough = new ArrayList<>();
|
||||
int w = aspectRatio.getWidth();
|
||||
int h = aspectRatio.getHeight();
|
||||
for (Size option : choices) {
|
||||
if (option.getHeight() == option.getWidth() * h / w &&
|
||||
option.getWidth() >= width && option.getHeight() >= height) {
|
||||
bigEnough.add(option);
|
||||
if (option.getWidth() <= maxWidth && option.getHeight() <= maxHeight &&
|
||||
option.getHeight() == option.getWidth() * h / w) {
|
||||
if (option.getWidth() >= textureViewWidth &&
|
||||
option.getHeight() >= textureViewHeight) {
|
||||
bigEnough.add(option);
|
||||
} else {
|
||||
notBigEnough.add(option);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Pick the smallest of those, assuming we found any
|
||||
// Pick the smallest of those big enough. If there is no one big enough, pick the
|
||||
// largest of those not big enough.
|
||||
if (bigEnough.size() > 0) {
|
||||
return Collections.min(bigEnough, new CompareSizesByArea());
|
||||
} else if (notBigEnough.size() > 0) {
|
||||
return Collections.max(notBigEnough, new CompareSizesByArea());
|
||||
} else {
|
||||
Log.e(TAG, "Couldn't find any suitable preview size");
|
||||
return choices[0];
|
||||
@@ -478,11 +506,57 @@ public class Camera2BasicFragment extends Fragment
|
||||
mImageReader.setOnImageAvailableListener(
|
||||
mOnImageAvailableListener, mBackgroundHandler);
|
||||
|
||||
// Find out if we need to swap dimension to get the preview size relative to sensor
|
||||
// coordinate.
|
||||
int displayRotation = activity.getWindowManager().getDefaultDisplay().getRotation();
|
||||
int sensorOrientation =
|
||||
characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION);
|
||||
boolean swappedDimensions = false;
|
||||
switch (displayRotation) {
|
||||
case Surface.ROTATION_0:
|
||||
case Surface.ROTATION_180:
|
||||
if (sensorOrientation == 90 || sensorOrientation == 270) {
|
||||
swappedDimensions = true;
|
||||
}
|
||||
break;
|
||||
case Surface.ROTATION_90:
|
||||
case Surface.ROTATION_270:
|
||||
if (sensorOrientation == 0 || sensorOrientation == 180) {
|
||||
swappedDimensions = true;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
Log.e(TAG, "Display rotation is invalid: " + displayRotation);
|
||||
}
|
||||
|
||||
Point displaySize = new Point();
|
||||
activity.getWindowManager().getDefaultDisplay().getSize(displaySize);
|
||||
int rotatedPreviewWidth = width;
|
||||
int rotatedPreviewHeight = height;
|
||||
int maxPreviewWidth = displaySize.x;
|
||||
int maxPreviewHeight = displaySize.y;
|
||||
|
||||
if (swappedDimensions) {
|
||||
rotatedPreviewWidth = height;
|
||||
rotatedPreviewHeight = width;
|
||||
maxPreviewWidth = displaySize.y;
|
||||
maxPreviewHeight = displaySize.x;
|
||||
}
|
||||
|
||||
if (maxPreviewWidth > MAX_PREVIEW_WIDTH) {
|
||||
maxPreviewWidth = MAX_PREVIEW_WIDTH;
|
||||
}
|
||||
|
||||
if (maxPreviewHeight > MAX_PREVIEW_HEIGHT) {
|
||||
maxPreviewHeight = MAX_PREVIEW_HEIGHT;
|
||||
}
|
||||
|
||||
// Danger, W.R.! Attempting to use too large a preview size could exceed the camera
|
||||
// bus' bandwidth limitation, resulting in gorgeous previews but the storage of
|
||||
// garbage capture data.
|
||||
mPreviewSize = chooseOptimalSize(map.getOutputSizes(SurfaceTexture.class),
|
||||
width, height, largest);
|
||||
rotatedPreviewWidth, rotatedPreviewHeight, maxPreviewWidth,
|
||||
maxPreviewHeight, largest);
|
||||
|
||||
// We fit the aspect ratio of TextureView to the size of preview we picked.
|
||||
int orientation = getResources().getConfiguration().orientation;
|
||||
|
||||
Reference in New Issue
Block a user