Fixes and improvements to Android Camera
This commit is contained in:
parent
1c92a2b782
commit
258014cd48
|
@ -1,8 +1,10 @@
|
||||||
package com.wix.RNCameraKit.camera;
|
package com.wix.RNCameraKit.camera;
|
||||||
|
|
||||||
|
import android.database.Cursor;
|
||||||
import android.graphics.Bitmap;
|
import android.graphics.Bitmap;
|
||||||
import android.graphics.BitmapFactory;
|
import android.graphics.BitmapFactory;
|
||||||
import android.hardware.Camera;
|
import android.hardware.Camera;
|
||||||
|
import android.net.Uri;
|
||||||
import android.os.AsyncTask;
|
import android.os.AsyncTask;
|
||||||
import android.os.Environment;
|
import android.os.Environment;
|
||||||
import android.provider.MediaStore;
|
import android.provider.MediaStore;
|
||||||
|
@ -93,11 +95,20 @@ public class CameraModule extends ReactContextBaseJavaModule {
|
||||||
protected Void doInBackground(byte[]... data) {
|
protected Void doInBackground(byte[]... data) {
|
||||||
Bitmap image = BitmapFactory.decodeByteArray(data[0], 0, data[0].length);
|
Bitmap image = BitmapFactory.decodeByteArray(data[0], 0, data[0].length);
|
||||||
String fileUri = MediaStore.Images.Media.insertImage(getReactApplicationContext().getContentResolver(), image, System.currentTimeMillis() + "", "");
|
String fileUri = MediaStore.Images.Media.insertImage(getReactApplicationContext().getContentResolver(), image, System.currentTimeMillis() + "", "");
|
||||||
Log.d("CameraKit", "Saved to: " + fileUri);
|
Log.d("CameraKit", "Saved to content uri: " + fileUri);
|
||||||
|
|
||||||
if (fileUri == null) {
|
if (fileUri == null) {
|
||||||
promise.reject("CameraKit", "Failed to save image to mediastore");
|
promise.reject("CameraKit", "Failed to save image to mediastore");
|
||||||
} else {
|
} else {
|
||||||
promise.resolve(fileUri);
|
|
||||||
|
Cursor cursor = getReactApplicationContext().getContentResolver().query(Uri.parse(fileUri), new String[] {MediaStore.Images.ImageColumns.DATA }, null, null, null);
|
||||||
|
cursor.moveToFirst();
|
||||||
|
String filePath = cursor.getString(0);
|
||||||
|
cursor.close();
|
||||||
|
promise.resolve(filePath);
|
||||||
|
|
||||||
|
Log.d("CameraKit", "Uri resolved to file path: " + fileUri);
|
||||||
|
|
||||||
CameraViewManager.initCamera();
|
CameraViewManager.initCamera();
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
|
|
|
@ -4,23 +4,28 @@ import android.hardware.Camera;
|
||||||
import android.view.SurfaceHolder;
|
import android.view.SurfaceHolder;
|
||||||
import android.view.SurfaceView;
|
import android.view.SurfaceView;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
|
import android.widget.FrameLayout;
|
||||||
|
|
||||||
import com.facebook.react.uimanager.ThemedReactContext;
|
import com.facebook.react.uimanager.ThemedReactContext;
|
||||||
|
|
||||||
import java.util.List;
|
import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by yedidyak on 04/07/2016.
|
* Created by yedidyak on 04/07/2016.
|
||||||
*/
|
*/
|
||||||
public class CameraView extends SurfaceView implements SurfaceHolder.Callback {
|
public class CameraView extends FrameLayout implements SurfaceHolder.Callback {
|
||||||
|
|
||||||
private ThemedReactContext context;
|
private ThemedReactContext context;
|
||||||
|
private SurfaceView surface;
|
||||||
|
|
||||||
public CameraView(ThemedReactContext context) {
|
public CameraView(ThemedReactContext context) {
|
||||||
super(context);
|
super(context);
|
||||||
this.context = context;
|
this.context = context;
|
||||||
this.getHolder().addCallback(this);
|
|
||||||
this.setOnClickListener(new OnClickListener() {
|
this.surface = new SurfaceView(context);
|
||||||
|
addView(surface, MATCH_PARENT, MATCH_PARENT);
|
||||||
|
surface.getHolder().addCallback(this);
|
||||||
|
surface.setOnClickListener(new OnClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
if (CameraViewManager.getCamera() != null) {
|
if (CameraViewManager.getCamera() != null) {
|
||||||
|
@ -38,6 +43,13 @@ public class CameraView extends SurfaceView implements SurfaceHolder.Callback {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
|
||||||
|
int actualPreviewWidth = getResources().getDisplayMetrics().widthPixels;
|
||||||
|
int actualPreviewHeight = getResources().getDisplayMetrics().heightPixels;
|
||||||
|
surface.layout(0, 0, actualPreviewWidth, actualPreviewHeight);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void surfaceCreated(SurfaceHolder holder) {
|
public void surfaceCreated(SurfaceHolder holder) {
|
||||||
CameraViewManager.setCameraView(this);
|
CameraViewManager.setCameraView(this);
|
||||||
|
@ -45,12 +57,13 @@ public class CameraView extends SurfaceView implements SurfaceHolder.Callback {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
|
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
|
||||||
CameraViewManager.updateCameraSize();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void surfaceDestroyed(SurfaceHolder holder) {}
|
public void surfaceDestroyed(SurfaceHolder holder) {}
|
||||||
|
|
||||||
|
|
||||||
|
public SurfaceHolder getHolder() {
|
||||||
|
return surface.getHolder();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,12 @@
|
||||||
package com.wix.RNCameraKit.camera;
|
package com.wix.RNCameraKit.camera;
|
||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.graphics.Point;
|
||||||
import android.hardware.Camera;
|
import android.hardware.Camera;
|
||||||
|
import android.view.Display;
|
||||||
import android.view.Surface;
|
import android.view.Surface;
|
||||||
import android.view.SurfaceHolder;
|
import android.view.WindowManager;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
import com.facebook.react.uimanager.SimpleViewManager;
|
import com.facebook.react.uimanager.SimpleViewManager;
|
||||||
|
@ -46,6 +49,7 @@ public class CameraViewManager extends SimpleViewManager<CameraView> {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void setCameraView(CameraView cameraView) {
|
public static void setCameraView(CameraView cameraView) {
|
||||||
|
if(CameraViewManager.cameraView == cameraView) return;
|
||||||
CameraViewManager.cameraView = cameraView;
|
CameraViewManager.cameraView = cameraView;
|
||||||
connectHolder();
|
connectHolder();
|
||||||
}
|
}
|
||||||
|
@ -83,7 +87,8 @@ public class CameraViewManager extends SimpleViewManager<CameraView> {
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
camera = Camera.open(currentCamera);
|
camera = Camera.open(currentCamera);
|
||||||
connectHolder();
|
setCameraDisplayOrientation(((Activity) reactContext.getBaseContext()), 0, camera);
|
||||||
|
updateCameraSize();
|
||||||
} catch (RuntimeException e) {
|
} catch (RuntimeException e) {
|
||||||
Toast.makeText(reactContext, "Cannot connect to Camera", Toast.LENGTH_SHORT).show();
|
Toast.makeText(reactContext, "Cannot connect to Camera", Toast.LENGTH_SHORT).show();
|
||||||
}
|
}
|
||||||
|
@ -95,9 +100,7 @@ public class CameraViewManager extends SimpleViewManager<CameraView> {
|
||||||
try {
|
try {
|
||||||
camera.stopPreview();
|
camera.stopPreview();
|
||||||
camera.setPreviewDisplay(cameraView.getHolder());
|
camera.setPreviewDisplay(cameraView.getHolder());
|
||||||
updateCameraSize();
|
|
||||||
camera.startPreview();
|
camera.startPreview();
|
||||||
setCameraDisplayOrientation(((Activity) reactContext.getBaseContext()), 0, camera);
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
@ -132,6 +135,9 @@ public class CameraViewManager extends SimpleViewManager<CameraView> {
|
||||||
} else { // back-facing
|
} else { // back-facing
|
||||||
result = (info.orientation - degrees + 360) % 360;
|
result = (info.orientation - degrees + 360) % 360;
|
||||||
}
|
}
|
||||||
|
Camera.Parameters parameters = camera.getParameters();
|
||||||
|
parameters.setRotation(result);
|
||||||
|
camera.setParameters(parameters);
|
||||||
camera.setDisplayOrientation(result);
|
camera.setDisplayOrientation(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -162,24 +168,24 @@ public class CameraViewManager extends SimpleViewManager<CameraView> {
|
||||||
return optimalSize;
|
return optimalSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void updateCameraSize() {
|
private static void updateCameraSize() {
|
||||||
try {
|
try {
|
||||||
Camera camera = CameraViewManager.getCamera();
|
Camera camera = CameraViewManager.getCamera();
|
||||||
if (camera == null || cameraView == null) return;
|
|
||||||
|
WindowManager wm = (WindowManager) reactContext.getSystemService(Context.WINDOW_SERVICE);
|
||||||
|
Display display = wm.getDefaultDisplay();
|
||||||
|
Point size = new Point();
|
||||||
|
display.getSize(size);
|
||||||
|
if (camera == null) return;
|
||||||
List<Camera.Size> supportedPreviewSizes = camera.getParameters().getSupportedPreviewSizes();
|
List<Camera.Size> supportedPreviewSizes = camera.getParameters().getSupportedPreviewSizes();
|
||||||
List<Camera.Size> supportedPictureSizes = camera.getParameters().getSupportedPictureSizes();
|
List<Camera.Size> supportedPictureSizes = camera.getParameters().getSupportedPictureSizes();
|
||||||
Camera.Size optimalSize = getOptimalPreviewSize(supportedPreviewSizes, cameraView.getWidth(), cameraView.getHeight());
|
Camera.Size optimalSize = getOptimalPreviewSize(supportedPreviewSizes, size.x, size.y);
|
||||||
Camera.Size optimalPictureSize = getOptimalPreviewSize(supportedPictureSizes, cameraView.getWidth(), cameraView.getHeight());
|
Camera.Size optimalPictureSize = getOptimalPreviewSize(supportedPictureSizes, size.x, size.y);
|
||||||
Camera.Parameters parameters = camera.getParameters();
|
Camera.Parameters parameters = camera.getParameters();
|
||||||
parameters.setPreviewSize(optimalSize.width, optimalSize.height);
|
parameters.setPreviewSize(optimalSize.width, optimalSize.height);
|
||||||
parameters.setPictureSize(optimalPictureSize.width, optimalPictureSize.height);
|
parameters.setPictureSize(optimalPictureSize.width, optimalPictureSize.height);
|
||||||
parameters.setFlashMode(CameraViewManager.getFlashMode());
|
parameters.setFlashMode(CameraViewManager.getFlashMode());
|
||||||
camera.setParameters(parameters);
|
camera.setParameters(parameters);
|
||||||
camera.startPreview();
|
} catch (RuntimeException e) {}
|
||||||
} catch (RuntimeException e) {
|
|
||||||
// CameraViewManager.initCamera();
|
|
||||||
// CameraViewManager.setCameraView(this);
|
|
||||||
// updateCameraSize();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
package com.wix.RNCameraKit.gallery;
|
package com.wix.RNCameraKit.gallery;
|
||||||
|
|
||||||
import android.app.Activity;
|
|
||||||
import android.database.Cursor;
|
import android.database.Cursor;
|
||||||
import android.graphics.Color;
|
import android.graphics.Color;
|
||||||
import android.graphics.drawable.Drawable;
|
import android.graphics.drawable.Drawable;
|
||||||
|
@ -10,9 +9,6 @@ import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
import android.widget.ImageView;
|
import android.widget.ImageView;
|
||||||
|
|
||||||
import com.facebook.react.bridge.ReactBridge;
|
|
||||||
import com.facebook.react.bridge.ReactContext;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.concurrent.LinkedBlockingDeque;
|
import java.util.concurrent.LinkedBlockingDeque;
|
||||||
|
|
|
@ -84,14 +84,6 @@ public class GalleryViewManager extends SimpleViewManager<GalleryView> {
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
// @ReactMethod
|
|
||||||
// public void refreshGalleryView(ReadableArray selectedImages, Promise promise) {
|
|
||||||
// view.refresh();
|
|
||||||
// setSelectedUris(view, selectedImages);
|
|
||||||
// promise.resolve(true);
|
|
||||||
// }
|
|
||||||
|
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
@Override
|
@Override
|
||||||
public Map<String, Integer> getCommandsMap() {
|
public Map<String, Integer> getCommandsMap() {
|
||||||
|
@ -102,7 +94,6 @@ public class GalleryViewManager extends SimpleViewManager<GalleryView> {
|
||||||
public void receiveCommand(GalleryView root, int commandId, @Nullable ReadableArray args) {
|
public void receiveCommand(GalleryView root, int commandId, @Nullable ReadableArray args) {
|
||||||
if (commandId == COMMAND_REFRESH_GALLERY) {
|
if (commandId == COMMAND_REFRESH_GALLERY) {
|
||||||
root.refresh();
|
root.refresh();
|
||||||
setSelectedUris(root, args.getArray(0));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue