Search in sources :

Example 1 with WritableNativeMap

use of com.facebook.react.bridge.WritableNativeMap in project react-native-camera by lwansbrough.

the class RCTCameraModule method captureWithOrientation.

private void captureWithOrientation(final ReadableMap options, final Promise promise, int deviceOrientation) {
    Camera camera = RCTCamera.getInstance().acquireCameraInstance(options.getInt("type"));
    if (null == camera) {
        promise.reject("No camera found.");
        return;
    }
    if (options.getInt("mode") == RCT_CAMERA_CAPTURE_MODE_VIDEO) {
        record(options, promise);
        return;
    }
    RCTCamera.getInstance().setCaptureQuality(options.getInt("type"), options.getString("quality"));
    if (options.hasKey("playSoundOnCapture") && options.getBoolean("playSoundOnCapture")) {
        MediaActionSound sound = new MediaActionSound();
        sound.play(MediaActionSound.SHUTTER_CLICK);
    }
    if (options.hasKey("quality")) {
        RCTCamera.getInstance().setCaptureQuality(options.getInt("type"), options.getString("quality"));
    }
    final Boolean shouldMirror = options.hasKey("mirrorImage") && options.getBoolean("mirrorImage");
    RCTCamera.getInstance().adjustCameraRotationToDeviceOrientation(options.getInt("type"), deviceOrientation);
    camera.setPreviewCallback(null);
    Camera.PictureCallback captureCallback = new Camera.PictureCallback() {

        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            if (shouldMirror) {
                data = mirrorImage(data);
                if (data == null) {
                    promise.reject("Error mirroring image");
                }
            }
            data = fixOrientation(data);
            camera.stopPreview();
            camera.startPreview();
            WritableMap response = new WritableNativeMap();
            switch(options.getInt("target")) {
                case RCT_CAMERA_CAPTURE_TARGET_MEMORY:
                    String encoded = Base64.encodeToString(data, Base64.DEFAULT);
                    response.putString("data", encoded);
                    promise.resolve(response);
                    break;
                case RCT_CAMERA_CAPTURE_TARGET_CAMERA_ROLL:
                    {
                        File cameraRollFile = getOutputCameraRollFile(MEDIA_TYPE_IMAGE);
                        if (cameraRollFile == null) {
                            promise.reject("Error creating media file.");
                            return;
                        }
                        Throwable error = writeDataToFile(data, cameraRollFile);
                        if (error != null) {
                            promise.reject(error);
                            return;
                        }
                        rewriteOrientation(cameraRollFile.getAbsolutePath());
                        addToMediaStore(cameraRollFile.getAbsolutePath());
                        response.putString("path", Uri.fromFile(cameraRollFile).toString());
                        promise.resolve(response);
                        break;
                    }
                case RCT_CAMERA_CAPTURE_TARGET_DISK:
                    {
                        File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
                        if (pictureFile == null) {
                            promise.reject("Error creating media file.");
                            return;
                        }
                        Throwable error = writeDataToFile(data, pictureFile);
                        if (error != null) {
                            promise.reject(error);
                            return;
                        }
                        rewriteOrientation(pictureFile.getAbsolutePath());
                        response.putString("path", Uri.fromFile(pictureFile).toString());
                        promise.resolve(response);
                        break;
                    }
                case RCT_CAMERA_CAPTURE_TARGET_TEMP:
                    {
                        File tempFile = getTempMediaFile(MEDIA_TYPE_IMAGE);
                        if (tempFile == null) {
                            promise.reject("Error creating media file.");
                            return;
                        }
                        Throwable error = writeDataToFile(data, tempFile);
                        if (error != null) {
                            promise.reject(error);
                        }
                        rewriteOrientation(tempFile.getAbsolutePath());
                        response.putString("path", Uri.fromFile(tempFile).toString());
                        promise.resolve(response);
                        break;
                    }
            }
            mSafeToCapture = true;
        }
    };
    if (mSafeToCapture) {
        try {
            camera.takePicture(null, null, captureCallback);
            mSafeToCapture = false;
        } catch (RuntimeException ex) {
            Log.e(TAG, "Couldn't capture photo.", ex);
        }
    }
}
Also used : WritableMap(com.facebook.react.bridge.WritableMap) WritableNativeMap(com.facebook.react.bridge.WritableNativeMap) Camera(android.hardware.Camera)

Example 2 with WritableNativeMap

use of com.facebook.react.bridge.WritableNativeMap in project react-native-fcm by evollu.

the class ReactNativeJson method convertJsonToMap.

public static WritableMap convertJsonToMap(JSONObject jsonObject) throws JSONException {
    WritableMap map = new WritableNativeMap();
    Iterator<String> iterator = jsonObject.keys();
    while (iterator.hasNext()) {
        String key = iterator.next();
        Object value = jsonObject.get(key);
        if (value instanceof JSONObject) {
            map.putMap(key, convertJsonToMap((JSONObject) value));
        } else if (value instanceof JSONArray) {
            map.putArray(key, convertJsonToArray((JSONArray) value));
        } else if (value instanceof Boolean) {
            map.putBoolean(key, (Boolean) value);
        } else if (value instanceof Integer) {
            map.putInt(key, (Integer) value);
        } else if (value instanceof Double) {
            map.putDouble(key, (Double) value);
        } else if (value instanceof String) {
            map.putString(key, (String) value);
        } else {
            map.putString(key, value.toString());
        }
    }
    return map;
}
Also used : WritableMap(com.facebook.react.bridge.WritableMap) JSONObject(org.json.JSONObject) WritableNativeMap(com.facebook.react.bridge.WritableNativeMap) JSONArray(org.json.JSONArray) JSONObject(org.json.JSONObject)

Example 3 with WritableNativeMap

use of com.facebook.react.bridge.WritableNativeMap in project react-native-camera by lwansbrough.

the class RCTCameraModule method releaseMediaRecorder.

/**
     * Release media recorder following video capture (or failure to start recording session).
     *
     * See "Capturing Videos" at https://developer.android.com/guide/topics/media/camera.html for
     * a guideline of steps and more information in general.
     */
private void releaseMediaRecorder() {
    // Must record at least a second or MediaRecorder throws exceptions on some platforms
    long duration = System.currentTimeMillis() - MRStartTime;
    if (duration < 1500) {
        try {
            Thread.sleep(1500 - duration);
        } catch (InterruptedException ex) {
            Log.e(TAG, "releaseMediaRecorder thread sleep error.", ex);
        }
    }
    // Release actual MediaRecorder instance.
    if (mMediaRecorder != null) {
        // Stop recording video.
        try {
            // stop the recording
            mMediaRecorder.stop();
        } catch (RuntimeException ex) {
            Log.e(TAG, "Media recorder stop error.", ex);
        }
        // Optionally, remove the configuration settings from the recorder.
        mMediaRecorder.reset();
        // Release the MediaRecorder.
        mMediaRecorder.release();
        // Reset variable.
        mMediaRecorder = null;
    }
    // MediaRecorder.prepare() call fails.
    if (mCamera != null) {
        mCamera.lock();
    }
    if (mRecordingPromise == null) {
        return;
    }
    File f = new File(mVideoFile.getPath());
    if (!f.exists()) {
        mRecordingPromise.reject(new RuntimeException("There is nothing recorded."));
        mRecordingPromise = null;
        return;
    }
    // so mediaplayer can play it
    f.setReadable(true, false);
    // so can clean it up
    f.setWritable(true, false);
    WritableMap response = new WritableNativeMap();
    switch(mRecordingOptions.getInt("target")) {
        case RCT_CAMERA_CAPTURE_TARGET_MEMORY:
            byte[] encoded = convertFileToByteArray(mVideoFile);
            response.putString("data", new String(encoded, Base64.DEFAULT));
            mRecordingPromise.resolve(response);
            f.delete();
            break;
        case RCT_CAMERA_CAPTURE_TARGET_CAMERA_ROLL:
            ContentValues values = new ContentValues();
            values.put(MediaStore.Video.Media.DATA, mVideoFile.getPath());
            values.put(MediaStore.Video.Media.TITLE, mRecordingOptions.hasKey("title") ? mRecordingOptions.getString("title") : "video");
            if (mRecordingOptions.hasKey("description")) {
                values.put(MediaStore.Video.Media.DESCRIPTION, mRecordingOptions.hasKey("description"));
            }
            if (mRecordingOptions.hasKey("latitude")) {
                values.put(MediaStore.Video.Media.LATITUDE, mRecordingOptions.getString("latitude"));
            }
            if (mRecordingOptions.hasKey("longitude")) {
                values.put(MediaStore.Video.Media.LONGITUDE, mRecordingOptions.getString("longitude"));
            }
            values.put(MediaStore.Video.Media.MIME_TYPE, "video/mp4");
            _reactContext.getContentResolver().insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values);
            addToMediaStore(mVideoFile.getAbsolutePath());
            response.putString("path", Uri.fromFile(mVideoFile).toString());
            mRecordingPromise.resolve(response);
            break;
        case RCT_CAMERA_CAPTURE_TARGET_TEMP:
        case RCT_CAMERA_CAPTURE_TARGET_DISK:
            response.putString("path", Uri.fromFile(mVideoFile).toString());
            mRecordingPromise.resolve(response);
    }
    mRecordingPromise = null;
}
Also used : ContentValues(android.content.ContentValues) WritableMap(com.facebook.react.bridge.WritableMap) WritableNativeMap(com.facebook.react.bridge.WritableNativeMap)

Aggregations

WritableMap (com.facebook.react.bridge.WritableMap)3 WritableNativeMap (com.facebook.react.bridge.WritableNativeMap)3 ContentValues (android.content.ContentValues)1 Camera (android.hardware.Camera)1 JSONArray (org.json.JSONArray)1 JSONObject (org.json.JSONObject)1