From 261edc56b8d2493c2b35613af1ddc44626aa2bf8 Mon Sep 17 00:00:00 2001 From: maning <154292322@qq.com> Date: Wed, 18 Sep 2019 10:41:29 +0800 Subject: [PATCH] =?UTF-8?q?=E6=89=AB=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../android/camera/AutoFocusManager.java | 2 +- .../camera/CameraConfigurationManager.java | 22 ++----- .../camera/CameraConfigurationUtils.java | 63 +++++++++++-------- 3 files changed, 44 insertions(+), 43 deletions(-) diff --git a/libraryzxing/src/main/java/com/google/zxing/client/android/camera/AutoFocusManager.java b/libraryzxing/src/main/java/com/google/zxing/client/android/camera/AutoFocusManager.java index 5824794..1fae206 100755 --- a/libraryzxing/src/main/java/com/google/zxing/client/android/camera/AutoFocusManager.java +++ b/libraryzxing/src/main/java/com/google/zxing/client/android/camera/AutoFocusManager.java @@ -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 FOCUS_MODES_CALLING_AF; static { FOCUS_MODES_CALLING_AF = new ArrayList<>(2); diff --git a/libraryzxing/src/main/java/com/google/zxing/client/android/camera/CameraConfigurationManager.java b/libraryzxing/src/main/java/com/google/zxing/client/android/camera/CameraConfigurationManager.java index 0adb9e4..dc0a0a1 100755 --- a/libraryzxing/src/main/java/com/google/zxing/client/android/camera/CameraConfigurationManager.java +++ b/libraryzxing/src/main/java/com/google/zxing/client/android/camera/CameraConfigurationManager.java @@ -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; @@ -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") diff --git a/libraryzxing/src/main/java/com/google/zxing/client/android/camera/CameraConfigurationUtils.java b/libraryzxing/src/main/java/com/google/zxing/client/android/camera/CameraConfigurationUtils.java index c9d3eff..d305959 100755 --- a/libraryzxing/src/main/java/com/google/zxing/client/android/camera/CameraConfigurationUtils.java +++ b/libraryzxing/src/main/java/com/google/zxing/client/android/camera/CameraConfigurationUtils.java @@ -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; @@ -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())) { @@ -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())) { @@ -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]); } } } @@ -189,7 +189,7 @@ public static void setMetering(Camera.Parameters parameters) { private static List 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) { @@ -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); } @@ -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 rawSupportedSizes = parameters.getSupportedPreviewSizes(); if (rawSupportedSizes == null) { @@ -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) { @@ -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; @@ -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; }