always use the proper aspect ratio at preview
This commit is contained in:
parent
557711c0ed
commit
4bb6d55244
|
@ -1,6 +1,7 @@
|
||||||
package com.simplemobiletools.camera;
|
package com.simplemobiletools.camera;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.graphics.Point;
|
||||||
import android.graphics.Rect;
|
import android.graphics.Rect;
|
||||||
import android.hardware.Camera;
|
import android.hardware.Camera;
|
||||||
import android.media.CamcorderProfile;
|
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 isSurfaceCreated;
|
||||||
private static boolean switchToVideoAsap;
|
private static boolean switchToVideoAsap;
|
||||||
private static boolean isVideoCaptureIntent;
|
private static boolean isVideoCaptureIntent;
|
||||||
|
private boolean setupPreviewAfterMeasure;
|
||||||
private static String curVideoPath;
|
private static String curVideoPath;
|
||||||
private static int lastClickX;
|
private static int lastClickX;
|
||||||
private static int lastClickY;
|
private static int lastClickY;
|
||||||
private static int initVideoRotation;
|
private static int initVideoRotation;
|
||||||
|
private static int navBarHeight;
|
||||||
|
private static Point screenSize;
|
||||||
private static Uri targetUri;
|
private static Uri targetUri;
|
||||||
|
|
||||||
public Preview(Context context) {
|
public Preview(Context context) {
|
||||||
|
@ -73,7 +77,10 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
|
||||||
isFlashEnabled = false;
|
isFlashEnabled = false;
|
||||||
isVideoMode = false;
|
isVideoMode = false;
|
||||||
isSurfaceCreated = false;
|
isSurfaceCreated = false;
|
||||||
|
setupPreviewAfterMeasure = false;
|
||||||
curVideoPath = "";
|
curVideoPath = "";
|
||||||
|
navBarHeight = Utils.getNavBarHeight(getResources());
|
||||||
|
screenSize = Utils.getScreenSize(activity);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void trySwitchToVideo() {
|
public void trySwitchToVideo() {
|
||||||
|
@ -111,6 +118,8 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
|
||||||
parameters = camera.getParameters();
|
parameters = camera.getParameters();
|
||||||
supportedPreviewSizes = parameters.getSupportedPreviewSizes();
|
supportedPreviewSizes = parameters.getSupportedPreviewSizes();
|
||||||
requestLayout();
|
requestLayout();
|
||||||
|
invalidate();
|
||||||
|
setupPreviewAfterMeasure = true;
|
||||||
|
|
||||||
final List<String> focusModes = parameters.getSupportedFocusModes();
|
final List<String> focusModes = parameters.getSupportedFocusModes();
|
||||||
if (focusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE))
|
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());
|
Log.e(TAG, "setCamera setPreviewDisplay " + e.getMessage());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
setupPreview();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
callback.setFlashAvailable(Utils.hasFlash(camera));
|
callback.setFlashAvailable(Utils.hasFlash(camera));
|
||||||
|
@ -359,25 +367,12 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
|
||||||
@Override
|
@Override
|
||||||
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
|
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
|
||||||
isSurfaceCreated = true;
|
isSurfaceCreated = true;
|
||||||
setupPreview();
|
|
||||||
|
|
||||||
if (isVideoMode) {
|
if (isVideoMode) {
|
||||||
initRecorder();
|
initRecorder();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setupPreview() {
|
|
||||||
canTakePicture = true;
|
|
||||||
if (camera != null && previewSize != null) {
|
|
||||||
parameters.setPreviewSize(previewSize.width, previewSize.height);
|
|
||||||
|
|
||||||
requestLayout();
|
|
||||||
|
|
||||||
camera.setParameters(parameters);
|
|
||||||
camera.startPreview();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void surfaceDestroyed(SurfaceHolder holder) {
|
public void surfaceDestroyed(SurfaceHolder holder) {
|
||||||
isSurfaceCreated = false;
|
isSurfaceCreated = false;
|
||||||
|
@ -388,6 +383,15 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
|
||||||
cleanupRecorder();
|
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() {
|
private void cleanupRecorder() {
|
||||||
if (recorder != null) {
|
if (recorder != null) {
|
||||||
if (isRecording) {
|
if (isRecording) {
|
||||||
|
@ -438,12 +442,25 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
||||||
final int width = resolveSize(getSuggestedMinimumWidth(), widthMeasureSpec);
|
setMeasuredDimension(screenSize.x, screenSize.y);
|
||||||
final int height = resolveSize(getSuggestedMinimumHeight(), heightMeasureSpec);
|
|
||||||
setMeasuredDimension(width, height);
|
|
||||||
|
|
||||||
if (supportedPreviewSizes != null) {
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,13 +1,16 @@
|
||||||
package com.simplemobiletools.camera;
|
package com.simplemobiletools.camera;
|
||||||
|
|
||||||
import android.Manifest;
|
import android.Manifest;
|
||||||
|
import android.app.Activity;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
import android.content.res.Resources;
|
import android.content.res.Resources;
|
||||||
|
import android.graphics.Point;
|
||||||
import android.hardware.Camera;
|
import android.hardware.Camera;
|
||||||
import android.media.MediaScannerConnection;
|
import android.media.MediaScannerConnection;
|
||||||
import android.os.Environment;
|
import android.os.Environment;
|
||||||
import android.support.v4.content.ContextCompat;
|
import android.support.v4.content.ContextCompat;
|
||||||
|
import android.view.Display;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
@ -97,6 +100,28 @@ public class Utils {
|
||||||
return sb.toString();
|
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) {
|
public static boolean hasCameraPermission(Context cxt) {
|
||||||
return ContextCompat.checkSelfPermission(cxt, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED;
|
return ContextCompat.checkSelfPermission(cxt, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED;
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,8 @@
|
||||||
android:id="@+id/viewHolder"
|
android:id="@+id/viewHolder"
|
||||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent">
|
android:layout_height="match_parent"
|
||||||
|
android:background="@android:color/black">
|
||||||
|
|
||||||
<SurfaceView
|
<SurfaceView
|
||||||
android:id="@+id/surfaceView"
|
android:id="@+id/surfaceView"
|
||||||
|
|
Loading…
Reference in New Issue