always use the proper aspect ratio at preview

This commit is contained in:
tibbi 2016-06-18 16:55:46 +02:00
parent 557711c0ed
commit 4bb6d55244
3 changed files with 62 additions and 19 deletions

View File

@ -1,6 +1,7 @@
package com.simplemobiletools.camera;
import android.content.Context;
import android.graphics.Point;
import android.graphics.Rect;
import android.hardware.Camera;
import android.media.CamcorderProfile;
@ -47,10 +48,13 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
private static boolean isSurfaceCreated;
private static boolean switchToVideoAsap;
private static boolean isVideoCaptureIntent;
private boolean setupPreviewAfterMeasure;
private static String curVideoPath;
private static int lastClickX;
private static int lastClickY;
private static int initVideoRotation;
private static int navBarHeight;
private static Point screenSize;
private static Uri targetUri;
public Preview(Context context) {
@ -73,7 +77,10 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
isFlashEnabled = false;
isVideoMode = false;
isSurfaceCreated = false;
setupPreviewAfterMeasure = false;
curVideoPath = "";
navBarHeight = Utils.getNavBarHeight(getResources());
screenSize = Utils.getScreenSize(activity);
}
public void trySwitchToVideo() {
@ -111,6 +118,8 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
parameters = camera.getParameters();
supportedPreviewSizes = parameters.getSupportedPreviewSizes();
requestLayout();
invalidate();
setupPreviewAfterMeasure = true;
final List<String> focusModes = parameters.getSupportedFocusModes();
if (focusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE))
@ -127,7 +136,6 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
Log.e(TAG, "setCamera setPreviewDisplay " + e.getMessage());
return false;
}
setupPreview();
}
callback.setFlashAvailable(Utils.hasFlash(camera));
@ -359,25 +367,12 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
isSurfaceCreated = true;
setupPreview();
if (isVideoMode) {
initRecorder();
}
}
private void setupPreview() {
canTakePicture = true;
if (camera != null && previewSize != null) {
parameters.setPreviewSize(previewSize.width, previewSize.height);
requestLayout();
camera.setParameters(parameters);
camera.startPreview();
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
isSurfaceCreated = false;
@ -388,6 +383,15 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
cleanupRecorder();
}
private void setupPreview() {
canTakePicture = true;
if (camera != null && previewSize != null) {
parameters.setPreviewSize(previewSize.width, previewSize.height);
camera.setParameters(parameters);
camera.startPreview();
}
}
private void cleanupRecorder() {
if (recorder != null) {
if (isRecording) {
@ -438,12 +442,25 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
final int width = resolveSize(getSuggestedMinimumWidth(), widthMeasureSpec);
final int height = resolveSize(getSuggestedMinimumHeight(), heightMeasureSpec);
setMeasuredDimension(width, height);
setMeasuredDimension(screenSize.x, screenSize.y);
if (supportedPreviewSizes != null) {
previewSize = getOptimalPreviewSize(supportedPreviewSizes, width, height);
previewSize = getOptimalPreviewSize(supportedPreviewSizes, screenSize.x, screenSize.y);
final LayoutParams lp = surfaceView.getLayoutParams();
if (screenSize.x > previewSize.height) {
final float ratio = (float) screenSize.x / previewSize.height;
lp.width = (int) (previewSize.height * ratio);
lp.height = (int) (previewSize.width * ratio);
} else {
lp.width = previewSize.height;
lp.height = previewSize.width;
}
if (setupPreviewAfterMeasure) {
setupPreviewAfterMeasure = false;
setupPreview();
}
}
}

View File

@ -1,13 +1,16 @@
package com.simplemobiletools.camera;
import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.graphics.Point;
import android.hardware.Camera;
import android.media.MediaScannerConnection;
import android.os.Environment;
import android.support.v4.content.ContextCompat;
import android.view.Display;
import android.widget.Toast;
import java.io.File;
@ -97,6 +100,28 @@ public class Utils {
return sb.toString();
}
public static Point getScreenSize(Activity activity) {
final Display display = activity.getWindowManager().getDefaultDisplay();
final Point size = new Point();
display.getSize(size);
size.y += getNavBarHeight(activity.getResources());
return size;
}
public static int getNavBarHeight(Resources res) {
int id = res.getIdentifier("navigation_bar_height", "dimen", "android");
if (id > 0 && hasNavBar(res)) {
return res.getDimensionPixelSize(id);
}
return 0;
}
public static boolean hasNavBar(Resources res) {
int id = res.getIdentifier("config_showNavigationBar", "bool", "android");
return id > 0 && res.getBoolean(id);
}
public static boolean hasCameraPermission(Context cxt) {
return ContextCompat.checkSelfPermission(cxt, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED;
}

View File

@ -3,7 +3,8 @@
android:id="@+id/viewHolder"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="match_parent"
android:background="@android:color/black">
<SurfaceView
android:id="@+id/surfaceView"