Skip to content

Commit

Permalink
扫码优化
Browse files Browse the repository at this point in the history
  • Loading branch information
maning0303 committed Sep 18, 2019
1 parent 28f874f commit 261edc5
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ final class AutoFocusManager implements Camera.AutoFocusCallback {

private static final String TAG = AutoFocusManager.class.getSimpleName();

private static final long AUTO_FOCUS_INTERVAL_MS = 2000L;
private static final long AUTO_FOCUS_INTERVAL_MS = 1200L;
private static final Collection<String> FOCUS_MODES_CALLING_AF;
static {
FOCUS_MODES_CALLING_AF = new ArrayList<>(2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,21 +108,11 @@ public void initFromCameraParameters(OpenCamera camera) {
display.getSize(theScreenResolution);
screenResolution = theScreenResolution;

/** 因为换成了竖屏显示,所以不替换屏幕宽高得出的预览图是变形的 */
Point screenResolutionForCamera = new Point();
screenResolutionForCamera.x = screenResolution.x;
screenResolutionForCamera.y = screenResolution.y;

if (screenResolution.x < screenResolution.y) {
screenResolutionForCamera.x = screenResolution.y;
screenResolutionForCamera.y = screenResolution.x;
}

Log.i(TAG, "Screen resolution in current orientation: " + screenResolution);
cameraResolution = CameraConfigurationUtils.findBestPreviewSizeValue(parameters, screenResolutionForCamera);
Log.i(TAG, "Camera resolution: " + cameraResolution);
bestPreviewSize = CameraConfigurationUtils.findBestPreviewSizeValue(parameters, screenResolutionForCamera);
Log.i(TAG, "Best available preview size: " + bestPreviewSize);
Log.i(TAG, ">>>>>>>Screen resolution in current orientation: " + screenResolution);
cameraResolution = CameraConfigurationUtils.findBestPreviewSizeValue(parameters, screenResolution);
Log.i(TAG, ">>>>>>>Camera resolution: " + cameraResolution);
bestPreviewSize = CameraConfigurationUtils.findBestPreviewSizeValue(parameters, screenResolution);
Log.i(TAG, ">>>>>>>Best available preview size: " + bestPreviewSize);

boolean isScreenPortrait = screenResolution.x < screenResolution.y;
boolean isPreviewSizePortrait = bestPreviewSize.x < bestPreviewSize.y;
Expand All @@ -132,7 +122,7 @@ public void initFromCameraParameters(OpenCamera camera) {
} else {
previewSizeOnScreen = new Point(bestPreviewSize.y, bestPreviewSize.x);
}
Log.i(TAG, "Preview size on screen: " + previewSizeOnScreen);
Log.i(TAG, ">>>>>>>Preview size on screen: " + previewSizeOnScreen);
}

@SuppressWarnings("deprecation")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public final class CameraConfigurationUtils {
private static final int MIN_PREVIEW_PIXELS = 480 * 320; // normal screen
private static final float MAX_EXPOSURE_COMPENSATION = 1.5f;
private static final float MIN_EXPOSURE_COMPENSATION = 0.0f;
private static final double MAX_ASPECT_DISTORTION = 0.15;
private static final double MAX_ASPECT_DISTORTION = 0.05;
private static final int MIN_FPS = 10;
private static final int MAX_FPS = 20;
private static final int AREA_PER_1000 = 400;
Expand All @@ -61,22 +61,22 @@ public static void setFocus(Camera.Parameters parameters,
if (autoFocus) {
if (safeMode || disableContinuous) {
focusMode = findSettableValue("focus mode",
supportedFocusModes,
Camera.Parameters.FOCUS_MODE_AUTO);
supportedFocusModes,
Camera.Parameters.FOCUS_MODE_AUTO);
} else {
focusMode = findSettableValue("focus mode",
supportedFocusModes,
Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE,
Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO,
Camera.Parameters.FOCUS_MODE_AUTO);
supportedFocusModes,
Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE,
Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO,
Camera.Parameters.FOCUS_MODE_AUTO);
}
}
// Maybe selected auto-focus but not available, so fall through here:
if (!safeMode && focusMode == null) {
focusMode = findSettableValue("focus mode",
supportedFocusModes,
Camera.Parameters.FOCUS_MODE_MACRO,
Camera.Parameters.FOCUS_MODE_EDOF);
supportedFocusModes,
Camera.Parameters.FOCUS_MODE_MACRO,
Camera.Parameters.FOCUS_MODE_EDOF);
}
if (focusMode != null) {
if (focusMode.equals(parameters.getFocusMode())) {
Expand All @@ -92,13 +92,13 @@ public static void setTorch(Camera.Parameters parameters, boolean on) {
String flashMode;
if (on) {
flashMode = findSettableValue("flash mode",
supportedFlashModes,
Camera.Parameters.FLASH_MODE_TORCH,
Camera.Parameters.FLASH_MODE_ON);
supportedFlashModes,
Camera.Parameters.FLASH_MODE_TORCH,
Camera.Parameters.FLASH_MODE_ON);
} else {
flashMode = findSettableValue("flash mode",
supportedFlashModes,
Camera.Parameters.FLASH_MODE_OFF);
supportedFlashModes,
Camera.Parameters.FLASH_MODE_OFF);
}
if (flashMode != null) {
if (flashMode.equals(parameters.getFlashMode())) {
Expand Down Expand Up @@ -159,7 +159,7 @@ public static void setBestPreviewFPS(Camera.Parameters parameters, int minFPS, i
} else {
Log.i(TAG, "Setting FPS range to " + Arrays.toString(suitableFPSRange));
parameters.setPreviewFpsRange(suitableFPSRange[Camera.Parameters.PREVIEW_FPS_MIN_INDEX],
suitableFPSRange[Camera.Parameters.PREVIEW_FPS_MAX_INDEX]);
suitableFPSRange[Camera.Parameters.PREVIEW_FPS_MAX_INDEX]);
}
}
}
Expand Down Expand Up @@ -189,7 +189,7 @@ public static void setMetering(Camera.Parameters parameters) {

private static List<Camera.Area> buildMiddleArea(int areaPer1000) {
return Collections.singletonList(
new Camera.Area(new Rect(-areaPer1000, -areaPer1000, areaPer1000, areaPer1000), 1));
new Camera.Area(new Rect(-areaPer1000, -areaPer1000, areaPer1000, areaPer1000), 1));
}

public static void setVideoStabilization(Camera.Parameters parameters) {
Expand All @@ -211,8 +211,8 @@ public static void setBarcodeSceneMode(Camera.Parameters parameters) {
return;
}
String sceneMode = findSettableValue("scene mode",
parameters.getSupportedSceneModes(),
Camera.Parameters.SCENE_MODE_BARCODE);
parameters.getSupportedSceneModes(),
Camera.Parameters.SCENE_MODE_BARCODE);
if (sceneMode != null) {
parameters.setSceneMode(sceneMode);
}
Expand Down Expand Up @@ -263,14 +263,14 @@ public static void setInvertColor(Camera.Parameters parameters) {
return;
}
String colorMode = findSettableValue("color effect",
parameters.getSupportedColorEffects(),
Camera.Parameters.EFFECT_NEGATIVE);
parameters.getSupportedColorEffects(),
Camera.Parameters.EFFECT_NEGATIVE);
if (colorMode != null) {
parameters.setColorEffect(colorMode);
}
}

public static Point findBestPreviewSizeValue(Camera.Parameters parameters, Point screenResolution) {
public static Point findBestPreviewSizeValue(Camera.Parameters parameters,final Point screenResolution) {

List<Camera.Size> rawSupportedSizes = parameters.getSupportedPreviewSizes();
if (rawSupportedSizes == null) {
Expand All @@ -282,6 +282,7 @@ public static Point findBestPreviewSizeValue(Camera.Parameters parameters, Point
return new Point(defaultSize.width, defaultSize.height);
}


if (Log.isLoggable(TAG, Log.INFO)) {
StringBuilder previewSizesString = new StringBuilder();
for (Camera.Size size : rawSupportedSizes) {
Expand All @@ -290,10 +291,16 @@ public static Point findBestPreviewSizeValue(Camera.Parameters parameters, Point
Log.i(TAG, "Supported preview sizes: " + previewSizesString);
}

double screenAspectRatio = screenResolution.x / (double) screenResolution.y;

double screenAspectRatio;
if(screenResolution.x < screenResolution.y){
screenAspectRatio = screenResolution.x / (double) screenResolution.y;
}else{
screenAspectRatio = screenResolution.y / (double) screenResolution.x;
}
Log.i(TAG, "screenAspectRatio: " + screenAspectRatio);
// Find a suitable size, with max resolution
int maxResolution = 0;

Camera.Size maxResPreviewSize = null;
for (Camera.Size size : rawSupportedSizes) {
int realWidth = size.width;
Expand All @@ -304,10 +311,14 @@ public static Point findBestPreviewSizeValue(Camera.Parameters parameters, Point
}

boolean isCandidatePortrait = realWidth < realHeight;
int maybeFlippedWidth = isCandidatePortrait ? realHeight : realWidth;
int maybeFlippedHeight = isCandidatePortrait ? realWidth : realHeight;
int maybeFlippedWidth = isCandidatePortrait ? realWidth: realHeight ;
int maybeFlippedHeight = isCandidatePortrait ? realHeight : realWidth;
Log.i(TAG, String.format("maybeFlipped:%d * %d",maybeFlippedWidth,maybeFlippedHeight));

double aspectRatio = maybeFlippedWidth / (double) maybeFlippedHeight;
Log.i(TAG, "aspectRatio: " + aspectRatio);
double distortion = Math.abs(aspectRatio - screenAspectRatio);
Log.i(TAG, "distortion: " + distortion);
if (distortion > MAX_ASPECT_DISTORTION) {
continue;
}
Expand Down

0 comments on commit 261edc5

Please sign in to comment.