Skip to content

Commit

Permalink
better algorithm of choosing width and height of the camera preview
Browse files Browse the repository at this point in the history
Signed-off-by: Umair Khan <omerjerk@gmail.com>
  • Loading branch information
omerjerk committed Jul 30, 2015
1 parent 1df62ce commit f56048f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
Binary file modified library/video_android.jar
Binary file not shown.
24 changes: 24 additions & 0 deletions src/in/omerjerk/processing/video/android/Capture.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public class Capture extends VideoBase implements CameraHandlerCallback {
private int selectedCamera = -1;

private CameraHandler mCameraHandler;

private boolean isPreviewSizeDefined = true;

public Capture(PApplet parent) {
this(parent, -1, -1, null);
Expand All @@ -45,6 +47,7 @@ public Capture(PApplet parent, int width, int height, String cameraName) {
if (width == -1 || height == -1) {
width = 720;
height = 1280;
isPreviewSizeDefined = false;
}
init(width, height, ARGB);
initalizeFrameBuffer();
Expand Down Expand Up @@ -225,6 +228,13 @@ public void startCamera(Integer cameraId) {
try {
log("Starting camera with camera id = " + cameraId);
mCamera = Camera.open(cameraId);
Camera.Parameters parameters = mCamera.getParameters();
if (!isPreviewSizeDefined) {
setPreviewSize(parameters, width);
} else {
parameters.setPreviewSize(width, height);
}
mCamera.setParameters(parameters);
mCamera.setDisplayOrientation(90);
} catch (Exception e) {
System.err.println("Couldn't open the camera");
Expand Down Expand Up @@ -257,4 +267,18 @@ public void startPreview() {
e.printStackTrace();
}
}

private void setPreviewSize(Camera.Parameters parameters, int expectedWidth) {
List<Camera.Size> sizes = parameters.getSupportedPreviewSizes();
int minDiff = 100000000;
Camera.Size minSize = null;
for (Camera.Size size : sizes) {
if (minDiff > Math.abs(expectedWidth - size.width)) {
minDiff = Math.abs(expectedWidth - size.width);
minSize = size;
}
}
Log.i("Capture", "Size not provided. Choosing " + minSize.width + "x" + minSize.height);
parameters.setPreviewSize(minSize.width, minSize.height);
}
}

0 comments on commit f56048f

Please sign in to comment.