本文主要是介绍android Zxing扫码二维码拉伸解决,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Zxing扫描二维码:
1、拉伸问题,大家都有解决办法,参考:https://blog.csdn.net/baidu_15079577/article/details/55048077
2、可能是源码库不同,我这边在 CameraConfigurationUtils 里面的 findBestPreviewSizeValue 修改尺寸
3、修改的位置和代码:
public static Point findBestPreviewSizeValue(Camera.Parameters parameters, Point screenResolution) {List<Camera.Size> rawSupportedSizes = parameters.getSupportedPreviewSizes();if (rawSupportedSizes == null) {Log.w(TAG, "Device returned no supported preview sizes; using default");Camera.Size defaultSize = parameters.getPreviewSize();if (defaultSize == null) {throw new IllegalStateException("Parameters contained no preview size!");}return new Point(defaultSize.width, defaultSize.height);}// Sort by size, descendingList<Camera.Size> supportedPreviewSizes = new ArrayList<>(rawSupportedSizes);Collections.sort(supportedPreviewSizes, new Comparator<Camera.Size>() {@Overridepublic int compare(Camera.Size a, Camera.Size b) {int aPixels = a.height * a.width;int bPixels = b.height * b.width;if (bPixels < aPixels) {return -1;}if (bPixels > aPixels) {return 1;}return 0;}});if (Log.isLoggable(TAG, Log.INFO)) {StringBuilder previewSizesString = new StringBuilder();for (Camera.Size supportedPreviewSize : supportedPreviewSizes) {previewSizesString.append(supportedPreviewSize.width).append('x').append(supportedPreviewSize.height).append(' ');}Log.i(TAG, "Supported preview sizes: " + previewSizesString);}double screenAspectRatio = screenResolution.x / (double) screenResolution.y;// Remove sizes that are unsuitableIterator<Camera.Size> it = supportedPreviewSizes.iterator();while (it.hasNext()) {Camera.Size supportedPreviewSize = it.next();int realWidth = supportedPreviewSize.width;int realHeight = supportedPreviewSize.height;if (realWidth * realHeight < MIN_PREVIEW_PIXELS) {it.remove();continue;}boolean isCandidatePortrait = realWidth < realHeight;int maybeFlippedWidth = isCandidatePortrait ? realHeight : realWidth;int maybeFlippedHeight = isCandidatePortrait ? realWidth : realHeight;double aspectRatio = maybeFlippedWidth / (double) maybeFlippedHeight;double distortion = Math.abs(aspectRatio - screenAspectRatio);if (distortion > MAX_ASPECT_DISTORTION) {it.remove();continue;}if (maybeFlippedWidth == screenResolution.x && maybeFlippedHeight == screenResolution.y) {Point exactPoint = new Point(realWidth, realHeight);Log.i(TAG, "Found preview size exactly matching screen size: " + exactPoint);return exactPoint;}}// If no exact match, use largest preview size. This was not a great idea on older devices because// of the additional computation needed. We're likely to get here on newer Android 4+ devices, where// the CPU is much more powerful.if (!supportedPreviewSizes.isEmpty()) {Camera.Size largestPreview = supportedPreviewSizes.get(0);Point largestSize = new Point(largestPreview.width, largestPreview.height);Log.i(TAG, "Using largest suitable preview size: " + largestSize);return largestSize;}// If there is nothing at all suitable, return current preview sizeCamera.Size defaultPreview = parameters.getPreviewSize();if (defaultPreview == null) {throw new IllegalStateException("Parameters contained no preview size!");}//修改的位置在这里,添加如下代码,替换defaultPreview returnPoint screenResolutionForCamera = new Point();screenResolutionForCamera.x = screenResolution.x;screenResolutionForCamera.y = screenResolution.y;// preview size is always something like 480*320, other 320*480if (screenResolution.x < screenResolution.y) {screenResolutionForCamera.x = screenResolution.y;screenResolutionForCamera.y = screenResolution.x;}Point cameraResolution = getCameraResolution(parameters, screenResolutionForCamera);Log.i(TAG, "No suitable preview sizes, using default: " + cameraResolution );return cameraResolution ;//Point defaultSize = new Point(defaultPreview.width, defaultPreview.height);//Log.i(TAG, "No suitable preview sizes, using default: " + defaultSize);//return defaultSize;
}
private static Point getCameraResolution(Camera.Parameters parameters, Point screenResolution) {String previewSizeValueString = parameters.get("preview-size-values");// saw this on Xperiaif (previewSizeValueString == null) {previewSizeValueString = parameters.get("preview-size-value");}Point cameraResolution = null;if (previewSizeValueString != null) {Log.e(TAG, "preview-size-values parameter: " + previewSizeValueString);cameraResolution = CameraConfigurationUtils.findBestPreviewSizeValue(parameters, screenResolution);}if (cameraResolution == null) {// Ensure that the camera resolution is a multiple of 8, as the screen may not be.cameraResolution = new Point((screenResolution.x >> 3) << 3, (screenResolution.y >> 3) << 3);}return cameraResolution;
}
这篇关于android Zxing扫码二维码拉伸解决的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!