continued refactor

This commit is contained in:
Yedidya Kennard 2016-07-19 11:48:17 +03:00
parent 57ca2cb114
commit 3a1687ecc7
3 changed files with 59 additions and 59 deletions

View File

@ -14,8 +14,6 @@ import java.util.List;
*/ */
public class CameraView extends SurfaceView implements SurfaceHolder.Callback { public class CameraView extends SurfaceView implements SurfaceHolder.Callback {
private ThemedReactContext context; private ThemedReactContext context;
public CameraView(ThemedReactContext context) { public CameraView(ThemedReactContext context) {
@ -35,65 +33,19 @@ public class CameraView extends SurfaceView implements SurfaceHolder.Callback {
}); });
} }
private void updateCameraSize() {
try {
Camera camera = CameraViewManager.getCamera();
if (camera == null) return;
List<Camera.Size> supportedPreviewSizes = camera.getParameters().getSupportedPreviewSizes();
List<Camera.Size> supportedPictureSizes = camera.getParameters().getSupportedPictureSizes();
Camera.Size optimalSize = getOptimalPreviewSize(supportedPreviewSizes, getWidth(), getHeight());
Camera.Size optimalPictureSize = getOptimalPreviewSize(supportedPictureSizes, getWidth(), getHeight());
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.setHolder(getHolder());
updateCameraSize();
}
}
@Override @Override
public void surfaceCreated(SurfaceHolder holder) { public void surfaceCreated(SurfaceHolder holder) {
CameraViewManager.setHolder(holder); CameraViewManager.setCameraView(this);
} }
@Override @Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
updateCameraSize(); CameraViewManager.updateCameraSize();
} }
@Override @Override
public void surfaceDestroyed(SurfaceHolder holder) {} public void surfaceDestroyed(SurfaceHolder holder) {}
private static Camera.Size getOptimalPreviewSize(List<Camera.Size> sizes, int w, int h) {
final double ASPECT_TOLERANCE = 0.1;
double targetRatio=(double)h / w;
if (sizes == null) return null;
Camera.Size optimalSize = null;
double minDiff = Double.MAX_VALUE;
int targetHeight = h;
for (Camera.Size size : sizes) {
double ratio = (double) size.width / size.height;
if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue;
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
if (optimalSize == null) {
minDiff = Double.MAX_VALUE;
for (Camera.Size size : sizes) {
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
}
return optimalSize;
}
} }

View File

@ -10,6 +10,7 @@ import com.facebook.react.uimanager.SimpleViewManager;
import com.facebook.react.uimanager.ThemedReactContext; import com.facebook.react.uimanager.ThemedReactContext;
import java.io.IOException; import java.io.IOException;
import java.util.List;
/** /**
* Created by yedidyak on 04/07/2016. * Created by yedidyak on 04/07/2016.
@ -19,7 +20,7 @@ public class CameraViewManager extends SimpleViewManager<CameraView> {
private static Camera camera = null; private static Camera camera = null;
private static int currentCamera = 0; private static int currentCamera = 0;
private static String flashMode = Camera.Parameters.FLASH_MODE_AUTO; private static String flashMode = Camera.Parameters.FLASH_MODE_AUTO;
private static SurfaceHolder holder; private static CameraView cameraView;
private static ThemedReactContext reactContext; private static ThemedReactContext reactContext;
public static Camera getCamera() { public static Camera getCamera() {
@ -44,8 +45,8 @@ public class CameraViewManager extends SimpleViewManager<CameraView> {
return new CameraView(reactContext); return new CameraView(reactContext);
} }
public static void setHolder(SurfaceHolder holder) { public static void setCameraView(CameraView cameraView) {
CameraViewManager.holder = holder; CameraViewManager.cameraView = cameraView;
connectHolder(); connectHolder();
} }
@ -72,6 +73,7 @@ public class CameraViewManager extends SimpleViewManager<CameraView> {
currentCamera++; currentCamera++;
currentCamera = currentCamera % Camera.getNumberOfCameras(); currentCamera = currentCamera % Camera.getNumberOfCameras();
initCamera(); initCamera();
return true; return true;
} }
@ -88,11 +90,12 @@ public class CameraViewManager extends SimpleViewManager<CameraView> {
} }
private static void connectHolder() { private static void connectHolder() {
if (camera == null || holder == null) return; if (camera == null || cameraView == null || cameraView.getHolder() == null) return;
try { try {
camera.stopPreview(); camera.stopPreview();
camera.setPreviewDisplay(holder); camera.setPreviewDisplay(cameraView.getHolder());
updateCameraSize();
camera.startPreview(); camera.startPreview();
setCameraDisplayOrientation(((Activity) reactContext.getBaseContext()), 0, camera); setCameraDisplayOrientation(((Activity) reactContext.getBaseContext()), 0, camera);
} catch (IOException e) { } catch (IOException e) {
@ -131,4 +134,52 @@ public class CameraViewManager extends SimpleViewManager<CameraView> {
} }
camera.setDisplayOrientation(result); camera.setDisplayOrientation(result);
} }
private static Camera.Size getOptimalPreviewSize(List<Camera.Size> sizes, int w, int h) {
final double ASPECT_TOLERANCE = 0.1;
double targetRatio=(double)h / w;
if (sizes == null) return null;
Camera.Size optimalSize = null;
double minDiff = Double.MAX_VALUE;
int targetHeight = h;
for (Camera.Size size : sizes) {
double ratio = (double) size.width / size.height;
if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue;
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
if (optimalSize == null) {
minDiff = Double.MAX_VALUE;
for (Camera.Size size : sizes) {
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
}
return optimalSize;
}
public static void updateCameraSize() {
try {
Camera camera = CameraViewManager.getCamera();
if (camera == null || cameraView == 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.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();
}
}
} }

View File

@ -1,11 +1,9 @@
import React, {Component} from 'react'; import React, {Component} from 'react';
import { import {
requireNativeComponent requireNativeComponent
//NativeModules
} from 'react-native'; } from 'react-native';
const GalleryView = requireNativeComponent('GalleryView', null); const GalleryView = requireNativeComponent('GalleryView', null);
//const GalleryViewManager = NativeModules.GalleryViewManager;
const ALL_PHOTOS = 'All Photos'; const ALL_PHOTOS = 'All Photos';
export default class CameraKitGalleryView extends Component { export default class CameraKitGalleryView extends Component {
@ -30,7 +28,6 @@ export default class CameraKitGalleryView extends Component {
} }
onTapImage(event) { onTapImage(event) {
console.log('ONTAPIMAGE!!!!', JSON.stringify(event));
if(this.props.onTapImage) { if(this.props.onTapImage) {
this.props.onTapImage(event.nativeEvent); this.props.onTapImage(event.nativeEvent);
} }