Fixes and improvements to Android Camera
This commit is contained in:
parent
1c92a2b782
commit
258014cd48
|
@ -1,8 +1,10 @@
|
|||
package com.wix.RNCameraKit.camera;
|
||||
|
||||
import android.database.Cursor;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.hardware.Camera;
|
||||
import android.net.Uri;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Environment;
|
||||
import android.provider.MediaStore;
|
||||
|
@ -93,11 +95,20 @@ public class CameraModule extends ReactContextBaseJavaModule {
|
|||
protected Void doInBackground(byte[]... data) {
|
||||
Bitmap image = BitmapFactory.decodeByteArray(data[0], 0, data[0].length);
|
||||
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) {
|
||||
promise.reject("CameraKit", "Failed to save image to mediastore");
|
||||
} 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();
|
||||
}
|
||||
return null;
|
||||
|
|
|
@ -4,23 +4,28 @@ import android.hardware.Camera;
|
|||
import android.view.SurfaceHolder;
|
||||
import android.view.SurfaceView;
|
||||
import android.view.View;
|
||||
import android.widget.FrameLayout;
|
||||
|
||||
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.
|
||||
*/
|
||||
public class CameraView extends SurfaceView implements SurfaceHolder.Callback {
|
||||
public class CameraView extends FrameLayout implements SurfaceHolder.Callback {
|
||||
|
||||
private ThemedReactContext context;
|
||||
private SurfaceView surface;
|
||||
|
||||
public CameraView(ThemedReactContext context) {
|
||||
super(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
|
||||
public void onClick(View v) {
|
||||
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
|
||||
public void surfaceCreated(SurfaceHolder holder) {
|
||||
CameraViewManager.setCameraView(this);
|
||||
|
@ -45,12 +57,13 @@ public class CameraView extends SurfaceView implements SurfaceHolder.Callback {
|
|||
|
||||
@Override
|
||||
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
|
||||
CameraViewManager.updateCameraSize();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void surfaceDestroyed(SurfaceHolder holder) {}
|
||||
|
||||
|
||||
|
||||
public SurfaceHolder getHolder() {
|
||||
return surface.getHolder();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,9 +1,12 @@
|
|||
package com.wix.RNCameraKit.camera;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.graphics.Point;
|
||||
import android.hardware.Camera;
|
||||
import android.view.Display;
|
||||
import android.view.Surface;
|
||||
import android.view.SurfaceHolder;
|
||||
import android.view.WindowManager;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.facebook.react.uimanager.SimpleViewManager;
|
||||
|
@ -46,6 +49,7 @@ public class CameraViewManager extends SimpleViewManager<CameraView> {
|
|||
}
|
||||
|
||||
public static void setCameraView(CameraView cameraView) {
|
||||
if(CameraViewManager.cameraView == cameraView) return;
|
||||
CameraViewManager.cameraView = cameraView;
|
||||
connectHolder();
|
||||
}
|
||||
|
@ -83,7 +87,8 @@ public class CameraViewManager extends SimpleViewManager<CameraView> {
|
|||
}
|
||||
try {
|
||||
camera = Camera.open(currentCamera);
|
||||
connectHolder();
|
||||
setCameraDisplayOrientation(((Activity) reactContext.getBaseContext()), 0, camera);
|
||||
updateCameraSize();
|
||||
} catch (RuntimeException e) {
|
||||
Toast.makeText(reactContext, "Cannot connect to Camera", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
@ -95,9 +100,7 @@ public class CameraViewManager extends SimpleViewManager<CameraView> {
|
|||
try {
|
||||
camera.stopPreview();
|
||||
camera.setPreviewDisplay(cameraView.getHolder());
|
||||
updateCameraSize();
|
||||
camera.startPreview();
|
||||
setCameraDisplayOrientation(((Activity) reactContext.getBaseContext()), 0, camera);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
@ -132,6 +135,9 @@ public class CameraViewManager extends SimpleViewManager<CameraView> {
|
|||
} else { // back-facing
|
||||
result = (info.orientation - degrees + 360) % 360;
|
||||
}
|
||||
Camera.Parameters parameters = camera.getParameters();
|
||||
parameters.setRotation(result);
|
||||
camera.setParameters(parameters);
|
||||
camera.setDisplayOrientation(result);
|
||||
}
|
||||
|
||||
|
@ -162,24 +168,24 @@ public class CameraViewManager extends SimpleViewManager<CameraView> {
|
|||
return optimalSize;
|
||||
}
|
||||
|
||||
public static void updateCameraSize() {
|
||||
private static void updateCameraSize() {
|
||||
try {
|
||||
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> supportedPictureSizes = camera.getParameters().getSupportedPictureSizes();
|
||||
Camera.Size optimalSize = getOptimalPreviewSize(supportedPreviewSizes, cameraView.getWidth(), cameraView.getHeight());
|
||||
Camera.Size optimalPictureSize = getOptimalPreviewSize(supportedPictureSizes, cameraView.getWidth(), cameraView.getHeight());
|
||||
Camera.Size optimalSize = getOptimalPreviewSize(supportedPreviewSizes, size.x, size.y);
|
||||
Camera.Size optimalPictureSize = getOptimalPreviewSize(supportedPictureSizes, size.x, size.y);
|
||||
Camera.Parameters parameters = camera.getParameters();
|
||||
parameters.setPreviewSize(optimalSize.width, optimalSize.height);
|
||||
parameters.setPictureSize(optimalPictureSize.width, optimalPictureSize.height);
|
||||
parameters.setFlashMode(CameraViewManager.getFlashMode());
|
||||
camera.setParameters(parameters);
|
||||
camera.startPreview();
|
||||
} catch (RuntimeException e) {
|
||||
// CameraViewManager.initCamera();
|
||||
// CameraViewManager.setCameraView(this);
|
||||
// updateCameraSize();
|
||||
}
|
||||
} catch (RuntimeException e) {}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
package com.wix.RNCameraKit.gallery;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.database.Cursor;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.drawable.Drawable;
|
||||
|
@ -10,9 +9,6 @@ import android.view.View;
|
|||
import android.view.ViewGroup;
|
||||
import android.widget.ImageView;
|
||||
|
||||
import com.facebook.react.bridge.ReactBridge;
|
||||
import com.facebook.react.bridge.ReactContext;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.concurrent.LinkedBlockingDeque;
|
||||
|
|
|
@ -83,15 +83,7 @@ public class GalleryViewManager extends SimpleViewManager<GalleryView> {
|
|||
.put("onTapImage", MapBuilder.of("registrationName", "onTapImage"))
|
||||
.build();
|
||||
}
|
||||
|
||||
// @ReactMethod
|
||||
// public void refreshGalleryView(ReadableArray selectedImages, Promise promise) {
|
||||
// view.refresh();
|
||||
// setSelectedUris(view, selectedImages);
|
||||
// promise.resolve(true);
|
||||
// }
|
||||
|
||||
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
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) {
|
||||
if (commandId == COMMAND_REFRESH_GALLERY) {
|
||||
root.refresh();
|
||||
setSelectedUris(root, args.getArray(0));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue