Skip to content

Commit

Permalink
Dev
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuriy Budiyev committed Aug 31, 2017
1 parent bf0c69d commit 51bc527
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import android.view.SurfaceHolder;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.client.android.camera.CameraConfigurationUtils;

import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -407,8 +406,9 @@ public void run() {
}
int orientation = Utils.getDisplayOrientation(mContext, cameraInfo);
boolean portrait = Utils.isPortrait(orientation);
Point previewSize = CameraConfigurationUtils.findBestPreviewSizeValue(parameters,
portrait ? new Point(mHeight, mWidth) : new Point(mWidth, mHeight));
Point previewSize =
Utils.findSuitablePreviewSize(parameters, portrait ? mHeight : mWidth,
portrait ? mWidth : mHeight);
parameters.setPreviewSize(previewSize.x, previewSize.y);
Point frameSize = Utils.getFrameSize(portrait ? previewSize.y : previewSize.x,
portrait ? previewSize.x : previewSize.y, mWidth, mHeight);
Expand Down
53 changes: 53 additions & 0 deletions src/main/java/com/budiyev/android/codescanner/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@

import com.google.zxing.client.android.camera.CameraConfigurationUtils;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

final class Utils {
Expand All @@ -44,6 +46,8 @@ final class Utils {
private static float PORTRAIT_HEIGHT_RATIO = 0.75f;
private static float LANDSCAPE_WIDTH_RATIO = 1.4f;
private static float LANDSCAPE_HEIGHT_RATIO = 0.625f;
private static float MAX_DISTORTION = 0.5f;
private static final int MIN_PREVIEW_PIXELS = 442368;

private Utils() {
}
Expand All @@ -64,6 +68,40 @@ public static Camera.Parameters optimizeParameters(@NonNull Camera.Parameters pa
return parameters;
}

@NonNull
public static Point findSuitablePreviewSize(@NonNull Camera.Parameters parameters,
int frameWidth, int frameHeight) {
List<Camera.Size> sizes = parameters.getSupportedPreviewSizes();
if (sizes == null || sizes.isEmpty()) {
return getDefaultPreviewSize(parameters);
}
Collections.sort(sizes, new CameraSizeComparator());
float frameRatio = (float) frameWidth / (float) frameHeight;
for (Camera.Size size : sizes) {
int width = size.width;
int height = size.height;
if (width * height < MIN_PREVIEW_PIXELS) {
continue;
}
float ratio = (float) width / (float) height;
float distortion = Math.abs(frameRatio - ratio);
if (distortion > MAX_DISTORTION) {
continue;
}
return new Point(width, height);
}
return getDefaultPreviewSize(parameters);
}

@NonNull
private static Point getDefaultPreviewSize(@NonNull Camera.Parameters parameters) {
Camera.Size defaultSize = parameters.getPreviewSize();
if (defaultSize == null) {
throw new RuntimeException("Can't get camera preview size");
}
return new Point(defaultSize.width, defaultSize.height);
}

public static int getDisplayOrientation(@NonNull Context context,
@NonNull Camera.CameraInfo cameraInfo) {
WindowManager windowManager =
Expand Down Expand Up @@ -212,4 +250,19 @@ private static int greatestCommonDivisor(int a, int b) {
}
return a + b;
}

private static final class CameraSizeComparator implements Comparator<Camera.Size> {
@Override
public 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;
} else if (bPixels > aPixels) {
return 1;
} else {
return 0;
}
}
}
}

0 comments on commit 51bc527

Please sign in to comment.