added support for android to crop image to preview.

This commit is contained in:
Marcus Andersson 2017-12-19 15:03:08 +01:00
parent ea859ca45f
commit c6ad435532
5 changed files with 64 additions and 0 deletions

View File

@ -76,6 +76,19 @@ public class MutableImage {
}
}
public void cropToPreview(float xPercentage, float yPercentage) throws ImageMutationFailedException {
int width = this.currentRepresentation.getWidth();
int height = this.currentRepresentation.getHeight();
int x = (int) Math.round(height * xPercentage);
int y = (int) Math.round(width * yPercentage);
int cropWidth = width - (y*2);
int cropHeight = height - (x*2);
this.currentRepresentation = Bitmap.createBitmap(this.currentRepresentation, y, x, cropWidth, cropHeight);
}
//see http://www.impulseadventure.com/photo/exif-orientation.html
private void rotate(int exifOrientation) throws ImageMutationFailedException {
final Matrix bitmapMatrix = new Matrix();

View File

@ -4,6 +4,7 @@
package com.lwansbrough.RCTCamera;
import android.graphics.drawable.GradientDrawable;
import android.hardware.Camera;
import android.media.CamcorderProfile;
import android.util.Log;
@ -73,6 +74,22 @@ public class RCTCamera {
return cameraInfo.previewHeight;
}
public float getPreviewPaddingHeight(int type) {
CameraInfoWrapper cameraInfo = _cameraInfos.get(type);
if (null == cameraInfo) {
return 0;
}
return cameraInfo.previewPaddingHeight;
}
public float getPreviewPaddingWidth(int type) {
CameraInfoWrapper cameraInfo = _cameraInfos.get(type);
if (null == cameraInfo) {
return 0;
}
return cameraInfo.previewPaddingWidth;
}
public Camera.Size getBestSize(List<Camera.Size> supportedSizes, int maxWidth, int maxHeight) {
Camera.Size bestSize = null;
for (Camera.Size size : supportedSizes) {
@ -418,6 +435,16 @@ public class RCTCamera {
}
}
public void setPreviewPadding(int type, float width, float height) {
CameraInfoWrapper cameraInfo = _cameraInfos.get(type);
if (cameraInfo == null) {
return;
}
cameraInfo.previewPaddingWidth = Math.abs(width);
cameraInfo.previewPaddingHeight = Math.abs(height);
}
private RCTCamera(int deviceOrientation) {
_cameras = new HashMap<>();
_cameraInfos = new HashMap<>();
@ -448,6 +475,9 @@ public class RCTCamera {
public int rotation = 0;
public int previewWidth = -1;
public int previewHeight = -1;
public int previewHeight = -1;
public float previewPaddingHeight = -1;
public float previewPaddingWidth = -1;
public CameraInfoWrapper(Camera.CameraInfo info) {
this.info = info;

View File

@ -580,6 +580,17 @@ public class RCTCameraModule extends ReactContextBaseJavaModule
}
}
try {
int type = options.getInt("type");
float paddingWidth = RCTCamera.getInstance().getPreviewPaddingWidth(type);
float paddingHeight = RCTCamera.getInstance().getPreviewPaddingHeight(type);
mutableImage.cropToPreview(paddingWidth, paddingHeight);
}
catch (MutableImage.ImageMutationFailedException e) {
promise.reject("Error cropping image to preview", e);
}
boolean shouldMirror = options.hasKey("mirrorImage") && options.getBoolean("mirrorImage");
if (shouldMirror) {
try {

View File

@ -6,6 +6,7 @@ package com.lwansbrough.RCTCamera;
import android.content.Context;
import android.hardware.SensorManager;
import android.util.Log;
import android.view.OrientationEventListener;
import android.view.ViewGroup;
import android.view.WindowManager;
@ -180,6 +181,11 @@ public class RCTCameraView extends ViewGroup {
int viewFinderPaddingX = (int) ((width - viewfinderWidth) / 2);
int viewFinderPaddingY = (int) ((height - viewfinderHeight) / 2);
float paddingXPercentage = (float)viewFinderPaddingX / (float)viewfinderWidth;
float paddingYPercentage = (float)viewFinderPaddingY / (float)viewfinderHeight;
RCTCamera.getInstance().setPreviewPadding(_viewFinder.getCameraType(), paddingXPercentage, paddingYPercentage);
this._viewFinder.layout(viewFinderPaddingX, viewFinderPaddingY, viewFinderPaddingX + viewfinderWidth, viewFinderPaddingY + viewfinderHeight);
this.postInvalidate(this.getLeft(), this.getTop(), this.getRight(), this.getBottom());
}

View File

@ -83,6 +83,10 @@ class RCTCameraViewFinder extends TextureView implements TextureView.SurfaceText
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
}
public int getCameraType() {
return _cameraType;
}
public double getRatio() {
int width = RCTCamera.getInstance().getPreviewWidth(this._cameraType);
int height = RCTCamera.getInstance().getPreviewHeight(this._cameraType);