2016-01-05 12:36:05 -08:00
|
|
|
/**
|
|
|
|
|
* Created by Fabrice Armisen (farmisen@gmail.com) on 1/3/16.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package com.lwansbrough.RCTCamera;
|
|
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
import android.graphics.SurfaceTexture;
|
|
|
|
|
import android.hardware.Camera;
|
|
|
|
|
import android.view.TextureView;
|
|
|
|
|
|
2016-02-18 12:55:28 +02:00
|
|
|
import java.util.List;
|
2016-01-05 12:36:05 -08:00
|
|
|
|
|
|
|
|
class RCTCameraViewFinder extends TextureView implements TextureView.SurfaceTextureListener {
|
|
|
|
|
private int _cameraType;
|
|
|
|
|
private SurfaceTexture _surfaceTexture;
|
|
|
|
|
private boolean _isStarting;
|
|
|
|
|
private boolean _isStopping;
|
|
|
|
|
private Camera _camera;
|
|
|
|
|
|
|
|
|
|
public RCTCameraViewFinder(Context context, int type) {
|
|
|
|
|
super(context);
|
|
|
|
|
this.setSurfaceTextureListener(this);
|
|
|
|
|
this._cameraType = type;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
|
|
|
|
|
_surfaceTexture = surface;
|
|
|
|
|
startCamera();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
|
|
|
|
|
_surfaceTexture = null;
|
|
|
|
|
stopCamera();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-27 20:49:46 -05:00
|
|
|
public double getRatio() {
|
2016-01-05 12:36:05 -08:00
|
|
|
int width = RCTCamera.getInstance().getPreviewWidth(this._cameraType);
|
|
|
|
|
int height = RCTCamera.getInstance().getPreviewHeight(this._cameraType);
|
|
|
|
|
return ((float) width) / ((float) height);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setCameraType(final int type) {
|
|
|
|
|
if (this._cameraType == type) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
new Thread(new Runnable() {
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
stopPreview();
|
|
|
|
|
_cameraType = type;
|
|
|
|
|
startPreview();
|
|
|
|
|
}
|
|
|
|
|
}).start();
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-20 21:31:23 +02:00
|
|
|
public void setCaptureQuality(String captureQuality) {
|
|
|
|
|
RCTCamera.getInstance().setCaptureQuality(_cameraType, captureQuality);
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-05 12:36:05 -08:00
|
|
|
public void setTorchMode(int torchMode) {
|
|
|
|
|
RCTCamera.getInstance().setTorchMode(_cameraType, torchMode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setFlashMode(int flashMode) {
|
|
|
|
|
RCTCamera.getInstance().setTorchMode(_cameraType, flashMode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void startPreview() {
|
|
|
|
|
if (_surfaceTexture != null) {
|
|
|
|
|
startCamera();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void stopPreview() {
|
|
|
|
|
if (_camera != null) {
|
|
|
|
|
stopCamera();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
synchronized private void startCamera() {
|
|
|
|
|
if (!_isStarting) {
|
|
|
|
|
_isStarting = true;
|
|
|
|
|
try {
|
|
|
|
|
_camera = RCTCamera.getInstance().acquireCameraInstance(_cameraType);
|
2016-01-28 10:19:22 -08:00
|
|
|
Camera.Parameters parameters = _camera.getParameters();
|
2016-03-20 21:31:23 +02:00
|
|
|
// set autofocus
|
2016-02-18 12:55:28 +02:00
|
|
|
List<String> focusModes = parameters.getSupportedFocusModes();
|
|
|
|
|
if (focusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) {
|
|
|
|
|
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
|
|
|
|
|
}
|
2016-03-20 21:31:23 +02:00
|
|
|
// set picture size
|
|
|
|
|
// defaults to max available size
|
2016-08-27 20:49:46 -05:00
|
|
|
Camera.Size optimalPictureSize = RCTCamera.getInstance().getBestSize(
|
|
|
|
|
parameters.getSupportedPictureSizes(),
|
|
|
|
|
Integer.MAX_VALUE,
|
|
|
|
|
Integer.MAX_VALUE
|
|
|
|
|
);
|
2016-03-20 21:31:23 +02:00
|
|
|
parameters.setPictureSize(optimalPictureSize.width, optimalPictureSize.height);
|
|
|
|
|
|
2016-01-28 10:19:22 -08:00
|
|
|
_camera.setParameters(parameters);
|
2016-01-05 12:36:05 -08:00
|
|
|
_camera.setPreviewTexture(_surfaceTexture);
|
|
|
|
|
_camera.startPreview();
|
|
|
|
|
} catch (NullPointerException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
stopCamera();
|
|
|
|
|
} finally {
|
|
|
|
|
_isStarting = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
synchronized private void stopCamera() {
|
|
|
|
|
if (!_isStopping) {
|
|
|
|
|
_isStopping = true;
|
|
|
|
|
try {
|
|
|
|
|
if (_camera != null) {
|
|
|
|
|
_camera.stopPreview();
|
|
|
|
|
RCTCamera.getInstance().releaseCameraInstance(_cameraType);
|
|
|
|
|
_camera = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
} finally {
|
|
|
|
|
_isStopping = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|