Search in sources :

Example 1 with ResolveTakenPictureAsyncTask

use of org.reactnative.camera.tasks.ResolveTakenPictureAsyncTask in project react-native-camera by react-native-community.

the class CameraModule method takePicture.

@ReactMethod
public void takePicture(final ReadableMap options, final int viewTag, final Promise promise) {
    final ReactApplicationContext context = getReactApplicationContext();
    final File cacheDirectory = mScopedContext.getCacheDirectory();
    UIManagerModule uiManager = context.getNativeModule(UIManagerModule.class);
    uiManager.addUIBlock(new UIBlock() {

        @Override
        public void execute(NativeViewHierarchyManager nativeViewHierarchyManager) {
            RNCameraView cameraView = (RNCameraView) nativeViewHierarchyManager.resolveView(viewTag);
            try {
                if (!Build.FINGERPRINT.contains("generic")) {
                    if (cameraView.isCameraOpened()) {
                        cameraView.takePicture(options, promise, cacheDirectory);
                    } else {
                        promise.reject("E_CAMERA_UNAVAILABLE", "Camera is not running");
                    }
                } else {
                    Bitmap image = RNCameraViewHelper.generateSimulatorPhoto(cameraView.getWidth(), cameraView.getHeight());
                    ByteArrayOutputStream stream = new ByteArrayOutputStream();
                    image.compress(Bitmap.CompressFormat.JPEG, 100, stream);
                    new ResolveTakenPictureAsyncTask(stream.toByteArray(), promise, options, cacheDirectory).execute();
                }
            } catch (Exception e) {
                promise.reject("E_CAMERA_BAD_VIEWTAG", "takePictureAsync: Expected a Camera component");
            }
        }
    });
}
Also used : UIBlock(com.facebook.react.uimanager.UIBlock) Bitmap(android.graphics.Bitmap) UIManagerModule(com.facebook.react.uimanager.UIManagerModule) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ReactApplicationContext(com.facebook.react.bridge.ReactApplicationContext) File(java.io.File) NativeViewHierarchyManager(com.facebook.react.uimanager.NativeViewHierarchyManager) ResolveTakenPictureAsyncTask(org.reactnative.camera.tasks.ResolveTakenPictureAsyncTask) ReactMethod(com.facebook.react.bridge.ReactMethod)

Aggregations

Bitmap (android.graphics.Bitmap)1 ReactApplicationContext (com.facebook.react.bridge.ReactApplicationContext)1 ReactMethod (com.facebook.react.bridge.ReactMethod)1 NativeViewHierarchyManager (com.facebook.react.uimanager.NativeViewHierarchyManager)1 UIBlock (com.facebook.react.uimanager.UIBlock)1 UIManagerModule (com.facebook.react.uimanager.UIManagerModule)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 ResolveTakenPictureAsyncTask (org.reactnative.camera.tasks.ResolveTakenPictureAsyncTask)1