Example usage for org.apache.cordova ExifHelper getOrientation

List of usage examples for org.apache.cordova ExifHelper getOrientation

Introduction

In this page you can find the example usage for org.apache.cordova ExifHelper getOrientation.

Prototype

public int getOrientation() 

Source Link

Usage

From source file:com.cooee.cordova.plugins.camera.CameraLauncher.java

License:Apache License

/**
 * Applies all needed transformation to the image received from the camera.
 *
 * @param destType In which form should we return the image
 * @param intent   An Intent, which can return result data to the caller (various data can be
 *                 attached to Intent "extras").
 */// ww  w  . j a  va 2 s .c o  m
private void processResultFromCamera(int destType, Intent intent) throws IOException {
    int rotate = 0;

    // Create an ExifHelper to save the exif data that is lost during compression
    ExifHelper exif = new ExifHelper();
    String sourcePath;
    try {
        if (allowEdit && croppedUri != null) {
            sourcePath = FileHelper.stripFileProtocol(croppedUri.toString());
        } else {
            sourcePath = getTempDirectoryPath() + "/.Pic.jpg";
        }

        //We don't support PNG, so let's not pretend we do
        exif.createInFile(getTempDirectoryPath() + "/.Pic.jpg");
        exif.readExifData();
        rotate = exif.getOrientation();

    } catch (IOException e) {
        e.printStackTrace();
    }

    Bitmap bitmap = null;
    Uri uri = null;

    // If sending base64 image back
    if (destType == DATA_URL) {
        if (croppedUri != null) {
            bitmap = getScaledBitmap(FileHelper.stripFileProtocol(croppedUri.toString()));
        } else {
            bitmap = getScaledBitmap(FileHelper.stripFileProtocol(imageUri.toString()));
        }
        if (bitmap == null) {
            // Try to get the bitmap from intent.
            bitmap = (Bitmap) intent.getExtras().get("data");
        }

        // Double-check the bitmap.
        if (bitmap == null) {
            Log.d(LOG_TAG, "I either have a null image path or bitmap");
            this.failPicture("Unable to create bitmap!");
            return;
        }

        if (rotate != 0 && this.correctOrientation) {
            bitmap = getRotatedBitmap(rotate, bitmap, exif);
        }

        this.processPicture(bitmap);
        checkForDuplicateImage(DATA_URL);
    }

    // If sending filename back
    else if (destType == FILE_URI || destType == NATIVE_URI) {
        uri = Uri.fromFile(new File(getTempDirectoryPath(), System.currentTimeMillis() + ".jpg"));

        if (this.saveToPhotoAlbum) {
            //Create a URI on the filesystem so that we can write the file.
            uri = Uri.fromFile(new File(getPicutresPath()));
        } else {
            uri = Uri.fromFile(new File(getTempDirectoryPath(), System.currentTimeMillis() + ".jpg"));
        }

        if (uri == null) {
            this.failPicture("Error capturing image - no media storage found.");
            return;
        }

        // If all this is true we shouldn't compress the image.
        if (this.targetHeight == -1 && this.targetWidth == -1 && this.mQuality == 100
                && !this.correctOrientation) {
            writeUncompressedImage(uri);

            this.callbackContext.success(uri.toString());
        } else {
            bitmap = getScaledBitmap(FileHelper.stripFileProtocol(imageUri.toString()));

            if (rotate != 0 && this.correctOrientation) {
                bitmap = getRotatedBitmap(rotate, bitmap, exif);
            }

            ContentResolver resolver = null;
            if (this.cordova.getActivity() != null) {
                resolver = this.cordova.getActivity().getContentResolver();
            } else if (this.cordova.getContext() != null) {
                resolver = this.cordova.getContext().getContentResolver();
            }

            // Add compressed version of captured image to returned media store Uri
            OutputStream os = resolver.openOutputStream(uri);
            bitmap.compress(Bitmap.CompressFormat.JPEG, this.mQuality, os);
            os.close();

            // Restore exif data to file
            if (this.encodingType == JPEG) {
                String exifPath;
                exifPath = uri.getPath();
                exif.createOutFile(exifPath);
                exif.writeExifData();
            }

            //Broadcast change to File System on MediaStore
            if (this.saveToPhotoAlbum) {
                refreshGallery(uri);
            }

            // Send Uri back to JavaScript for viewing image
            this.callbackContext.success(uri.toString());

        }
    } else {
        throw new IllegalStateException();
    }

    this.cleanup(FILE_URI, this.imageUri, uri, bitmap);
    bitmap = null;
}

From source file:com.facilityhero.plugin.camera.CameraLauncher.java

License:Apache License

/**
 * Called when the camera view exits./*from  w ww.ja v a 2  s.co m*/
 *
 * @param requestCode       The request code originally supplied to startActivityForResult(),
 *                          allowing you to identify who this result came from.
 * @param resultCode        The integer result code returned by the child activity through its setResult().
 * @param intent            An Intent, which can return result data to the caller (various data can be attached to Intent "extras").
 */
public void onActivityResult(int requestCode, int resultCode, Intent intent) {

    // Get src and dest types from request code
    int srcType = (requestCode / 16) - 1;
    int destType = (requestCode % 16) - 1;
    int rotate = 0;

    // If CAMERA
    if (srcType == CAMERA) {
        // If image available
        if (resultCode == Activity.RESULT_OK) {
            try {
                // Create an ExifHelper to save the exif data that is lost during compression
                ExifHelper exif = new ExifHelper();
                try {
                    if (this.encodingType == JPEG) {
                        exif.createInFile(getTempDirectoryPath() + "/.Pic.jpg");
                        exif.readExifData();
                        rotate = exif.getOrientation();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }

                Bitmap bitmap = null;
                Uri uri = null;

                // If sending base64 image back
                if (destType == DATA_URL) {
                    bitmap = getScaledBitmap(FileHelper.stripFileProtocol(imageUri.toString()));
                    if (bitmap == null) {
                        // Try to get the bitmap from intent.
                        bitmap = (Bitmap) intent.getExtras().get("data");
                    }

                    // Double-check the bitmap.
                    if (bitmap == null) {
                        Log.d(LOG_TAG, "I either have a null image path or bitmap");
                        this.failPicture("Unable to create bitmap!");
                        return;
                    }

                    if (rotate != 0 && this.correctOrientation) {
                        bitmap = getRotatedBitmap(rotate, bitmap, exif);
                    }

                    this.processPicture(bitmap);
                    checkForDuplicateImage(DATA_URL);
                }

                // If sending filename back
                else if (destType == FILE_URI || destType == NATIVE_URI) {
                    if (this.saveToPhotoAlbum) {
                        Uri inputUri = getUriFromMediaStore();
                        //Just because we have a media URI doesn't mean we have a real file, we need to make it
                        uri = Uri.fromFile(new File(FileHelper.getRealPath(inputUri, this.cordova)));
                    } else {
                        uri = Uri.fromFile(
                                new File(getTempDirectoryPath(), System.currentTimeMillis() + ".jpg"));
                    }

                    if (uri == null) {
                        this.failPicture("Error capturing image - no media storage found.");
                    }

                    // If all this is true we shouldn't compress the image.
                    if (this.targetHeight == -1 && this.targetWidth == -1 && this.mQuality == 100
                            && !this.correctOrientation) {
                        writeUncompressedImage(uri);

                        this.callbackContext.success(uri.toString());
                    } else {
                        bitmap = getScaledBitmap(FileHelper.stripFileProtocol(imageUri.toString()));

                        if (rotate != 0 && this.correctOrientation) {
                            bitmap = getRotatedBitmap(rotate, bitmap, exif);
                        }

                        // Add compressed version of captured image to returned media store Uri
                        OutputStream os = this.cordova.getActivity().getContentResolver().openOutputStream(uri);
                        bitmap.compress(Bitmap.CompressFormat.JPEG, this.mQuality, os);
                        os.close();

                        // Restore exif data to file
                        if (this.encodingType == JPEG) {
                            String exifPath;
                            if (this.saveToPhotoAlbum) {
                                exifPath = FileHelper.getRealPath(uri, this.cordova);
                            } else {
                                exifPath = uri.getPath();
                            }
                            exif.createOutFile(exifPath);
                            exif.writeExifData();
                        }

                    }
                    // Send Uri back to JavaScript for viewing image
                    this.callbackContext.success(uri.toString());
                }

                this.cleanup(FILE_URI, this.imageUri, uri, bitmap);
                bitmap = null;

            } catch (IOException e) {
                e.printStackTrace();
                this.failPicture("Error capturing image.");
            }
        }

        // If cancelled
        else if (resultCode == Activity.RESULT_CANCELED) {
            this.failPicture("Camera cancelled.");
        }

        // If something else
        else {
            this.failPicture("Did not complete!");
        }
    }

    // If retrieving photo from library
    else if ((srcType == PHOTOLIBRARY) || (srcType == SAVEDPHOTOALBUM)) {
        if (resultCode == Activity.RESULT_OK) {
            Uri uri = intent.getData();

            // If you ask for video or all media type you will automatically get back a file URI
            // and there will be no attempt to resize any returned data
            if (this.mediaType != PICTURE) {
                String filePath = FileHelper.getRealPath(uri, cordova);
                this.callbackContext.success("file://" + filePath);
            } else {
                // This is a special case to just return the path as no scaling,
                // rotating, nor compressing needs to be done
                if (this.targetHeight == -1 && this.targetWidth == -1
                        && (destType == FILE_URI || destType == NATIVE_URI) && !this.correctOrientation) {
                    String filePath = FileHelper.getRealPath(uri, cordova);
                    this.callbackContext.success("file://" + filePath);
                    //this.callbackContext.success(uri.toString());
                } else {
                    String uriString = uri.toString();
                    // Get the path to the image. Makes loading so much easier.
                    String mimeType = FileHelper.getMimeType(uriString, this.cordova);
                    // If we don't have a valid image so quit.
                    if (!("image/jpeg".equalsIgnoreCase(mimeType) || "image/png".equalsIgnoreCase(mimeType))) {
                        Log.d(LOG_TAG, "I either have a null image path or bitmap");
                        this.failPicture("Unable to retrieve path to picture!");
                        return;
                    }
                    Bitmap bitmap = null;
                    try {
                        bitmap = getScaledBitmap(uriString);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    if (bitmap == null) {
                        Log.d(LOG_TAG, "I either have a null image path or bitmap");
                        this.failPicture("Unable to create bitmap!");
                        return;
                    }

                    if (this.correctOrientation) {
                        rotate = getImageOrientation(uri);
                        if (rotate != 0) {
                            Matrix matrix = new Matrix();
                            matrix.setRotate(rotate);
                            bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(),
                                    matrix, true);
                        }
                    }

                    // If sending base64 image back
                    if (destType == DATA_URL) {
                        this.processPicture(bitmap);
                    }

                    // If sending filename back
                    else if (destType == FILE_URI || destType == NATIVE_URI) {
                        // Do we need to scale the returned file
                        if (this.targetHeight > 0 && this.targetWidth > 0) {
                            try {
                                // Create an ExifHelper to save the exif data that is lost during compression
                                String resizePath = getTempDirectoryPath() + "/resize.jpg";
                                // Some content: URIs do not map to file paths (e.g. picasa).
                                String realPath = FileHelper.getRealPath(uri, this.cordova);
                                ExifHelper exif = new ExifHelper();
                                if (realPath != null && this.encodingType == JPEG) {
                                    try {
                                        exif.createInFile(realPath);
                                        exif.readExifData();
                                        rotate = exif.getOrientation();
                                    } catch (IOException e) {
                                        e.printStackTrace();
                                    }
                                }

                                OutputStream os = new FileOutputStream(resizePath);
                                bitmap.compress(Bitmap.CompressFormat.JPEG, this.mQuality, os);
                                os.close();

                                // Restore exif data to file
                                if (realPath != null && this.encodingType == JPEG) {
                                    exif.createOutFile(resizePath);
                                    exif.writeExifData();
                                }

                                // The resized image is cached by the app in order to get around this and not have to delete you
                                // application cache I'm adding the current system time to the end of the file url.
                                this.callbackContext
                                        .success("file://" + resizePath + "?" + System.currentTimeMillis());
                            } catch (Exception e) {
                                e.printStackTrace();
                                this.failPicture("Error retrieving image.");
                            }
                        } else {
                            String filePath = FileHelper.getRealPath(uri, cordova);
                            this.callbackContext.success("file://" + filePath);
                        }
                    }
                    if (bitmap != null) {
                        bitmap.recycle();
                        bitmap = null;
                    }
                    System.gc();
                }
            }
        } else if (resultCode == Activity.RESULT_CANCELED) {
            this.failPicture("Selection cancelled.");
        } else {
            this.failPicture("Selection did not complete!");
        }
    }
}

From source file:com.laanto.it.app.camera.CameraLauncher.java

License:Apache License

/**
 * Applies all needed transformation to the image received from the camera.
 *
 * @param destType          In which form should we return the image
 * @param intent            An Intent, which can return result data to the caller (various data can be attached to Intent "extras").
 *///from w ww.j a va  2  s.c o  m
private void processResultFromCamera(int destType, Intent intent) throws IOException {
    int rotate = 0;

    // Create an ExifHelper to save the exif data that is lost during compression
    ExifHelper exif = new ExifHelper();
    String sourcePath;
    try {
        if (allowEdit && croppedUri != null) {
            sourcePath = FileHelper.stripFileProtocol(croppedUri.toString());
        } else {
            sourcePath = getTempDirectoryPath() + "/.Pic.jpg";
        }

        //We don't support PNG, so let's not pretend we do
        exif.createInFile(getTempDirectoryPath() + "/.Pic.jpg");
        exif.readExifData();
        rotate = exif.getOrientation();

    } catch (IOException e) {
        e.printStackTrace();
    }

    Bitmap bitmap = null;
    Uri uri = null;

    // If sending base64 image back
    if (destType == DATA_URL) {
        if (croppedUri != null) {
            bitmap = getScaledBitmap(FileHelper.stripFileProtocol(croppedUri.toString()));
        } else {
            bitmap = getScaledBitmap(FileHelper.stripFileProtocol(imageUri.toString()));
        }
        if (bitmap == null) {
            // Try to get the bitmap from intent.
            bitmap = (Bitmap) intent.getExtras().get("data");
        }

        // Double-check the bitmap.
        if (bitmap == null) {
            Log.d(LOG_TAG, "I either have a null image path or bitmap");
            this.failPicture("Unable to create bitmap!");
            return;
        }

        if (rotate != 0 && this.correctOrientation) {
            bitmap = getRotatedBitmap(rotate, bitmap, exif);
        }

        this.processPicture(bitmap);
        checkForDuplicateImage(DATA_URL);
    }

    // If sending filename back
    else if (destType == FILE_URI || destType == NATIVE_URI) {
        uri = Uri.fromFile(new File(getTempDirectoryPath(), System.currentTimeMillis() + ".jpg"));

        if (this.saveToPhotoAlbum) {
            //Create a URI on the filesystem so that we can write the file.
            uri = Uri.fromFile(new File(getPicutresPath()));
        } else {
            uri = Uri.fromFile(new File(getTempDirectoryPath(), System.currentTimeMillis() + ".jpg"));
        }

        if (uri == null) {
            this.failPicture("Error capturing image - no media storage found.");
            return;
        }

        // If all this is true we shouldn't compress the image.
        if (this.targetHeight == -1 && this.targetWidth == -1 && this.mQuality == 100
                && !this.correctOrientation) {
            writeUncompressedImage(uri);

            this.callbackContext.success(uri.toString());
        } else {
            bitmap = getScaledBitmap(FileHelper.stripFileProtocol(imageUri.toString()));

            if (rotate != 0 && this.correctOrientation) {
                bitmap = getRotatedBitmap(rotate, bitmap, exif);
            }

            // Add compressed version of captured image to returned media store Uri
            OutputStream os = this.cordova.getActivity().getContentResolver().openOutputStream(uri);
            bitmap.compress(Bitmap.CompressFormat.JPEG, this.mQuality, os);
            os.close();

            // Restore exif data to file
            if (this.encodingType == JPEG) {
                String exifPath;
                exifPath = uri.getPath();
                exif.createOutFile(exifPath);
                exif.writeExifData();
            }

            //Broadcast change to File System on MediaStore
            if (this.saveToPhotoAlbum) {
                refreshGallery(uri);
            }

            // Send Uri back to JavaScript for viewing image
            this.callbackContext.success(uri.toString());

        }
    } else {
        throw new IllegalStateException();
    }

    this.cleanup(FILE_URI, this.imageUri, uri, bitmap);
    bitmap = null;
}

From source file:com.logilite.vision.camera.CameraLauncher.java

License:Apache License

/**
 * Applies all needed transformation to the image received from the camera.
 *
 * @param destType          In which form should we return the image
 * @param intent            An Intent, which can return result data to the caller (various data can be attached to Intent "extras").
 *//*from w w  w  . j av a2  s . c  o m*/
private void processResultFromCamera(int destType, Intent intent) throws IOException {
    int rotate = 0;

    // Create an ExifHelper to save the exif data that is lost during compression
    ExifHelper exif = new ExifHelper();
    try {
        if (this.encodingType == JPEG) {
            exif.createInFile(getTempDirectoryPath() + "/.Pic.jpg");
            exif.readExifData();
            rotate = exif.getOrientation();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    Bitmap bitmap = null;
    Uri uri = null;

    // If sending base64 image back
    if (destType == DATA_URL) {
        bitmap = getScaledBitmap(FileHelper.stripFileProtocol(imageUri.toString()));
        if (bitmap == null) {
            // Try to get the bitmap from intent.
            bitmap = (Bitmap) intent.getExtras().get("data");
        }

        // Double-check the bitmap.
        if (bitmap == null) {
            Log.d(LOG_TAG, "I either have a null image path or bitmap");
            this.failPicture("Unable to create bitmap!");
            return;
        }

        if (rotate != 0 && this.correctOrientation) {
            bitmap = getRotatedBitmap(rotate, bitmap, exif);
        }

        this.processPicture(bitmap);
        checkForDuplicateImage(DATA_URL);
    }

    // If sending filename back
    else if (destType == FILE_URI || destType == NATIVE_URI) {
        if (this.saveToPhotoAlbum) {
            Uri inputUri = getUriFromMediaStore();
            //Just because we have a media URI doesn't mean we have a real file, we need to make it
            uri = Uri.fromFile(new File(FileHelper.getRealPath(inputUri, this.cordova)));
        } else {
            uri = Uri.fromFile(new File(getTempDirectoryPath(), System.currentTimeMillis() + ".jpg"));
        }

        if (uri == null) {
            this.failPicture("Error capturing image - no media storage found.");
        }

        // If all this is true we shouldn't compress the image.
        if (this.targetHeight == -1 && this.targetWidth == -1 && this.mQuality == 100
                && !this.correctOrientation) {
            writeUncompressedImage(uri);

            this.callbackContext.success(uri.toString());
        } else {
            bitmap = getScaledBitmap(FileHelper.stripFileProtocol(imageUri.toString()));

            if (rotate != 0 && this.correctOrientation) {
                bitmap = getRotatedBitmap(rotate, bitmap, exif);
            }

            // Add compressed version of captured image to returned media store Uri
            OutputStream os = this.cordova.getActivity().getContentResolver().openOutputStream(uri);
            bitmap.compress(Bitmap.CompressFormat.JPEG, this.mQuality, os);
            os.close();

            // Restore exif data to file
            if (this.encodingType == JPEG) {
                String exifPath;
                if (this.saveToPhotoAlbum) {
                    exifPath = FileHelper.getRealPath(uri, this.cordova);
                } else {
                    exifPath = uri.getPath();
                }
                exif.createOutFile(exifPath);
                exif.writeExifData();
            }

        }
        // Send Uri back to JavaScript for viewing image
        this.callbackContext.success(uri.toString());
    }

    this.cleanup(FILE_URI, this.imageUri, uri, bitmap);
    bitmap = null;
}

From source file:com.tuxpan.foregroundcameragalleryplugin.ForegroundCameraLauncher.java

License:Apache License

/**
 * Called when the camera view exits.//from w  w  w .j  a v  a  2s.  c o  m
 *
 * @param requestCode       The request code originally supplied to startActivityForResult(),
 *                          allowing you to identify who this result came from.
 * @param resultCode        The integer result code returned by the child activity through its setResult().
 * @param intent            An Intent, which can return result data to the caller (various data can be attached to Intent "extras").
 */
public void onActivityResult(int requestCode, int resultCode, Intent intent) {

    // Get src and dest types from request code
    int srcType = (requestCode / 16) - 1;
    int destType = (requestCode % 16) - 1;
    int rotate = 0;

    // If CAMERA
    if (srcType == CAMERA) {
        // If image available
        if (resultCode == Activity.RESULT_OK) {
            try {
                // Create an ExifHelper to save the exif data that is lost during compression
                ExifHelper exif = new ExifHelper();
                try {
                    if (this.encodingType == JPEG) {
                        exif.createInFile(getTempDirectoryPath() + "/.Pic.jpg");
                        exif.readExifData();
                        rotate = exif.getOrientation();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }

                Bitmap bitmap = null;
                Uri uri = null;

                // If sending base64 image back
                if (destType == DATA_URL) {
                    bitmap = getScaledBitmap(FileHelper.stripFileProtocol(imageUri.toString()));
                    if (bitmap == null) {
                        // Try to get the bitmap from intent.
                        bitmap = (Bitmap) intent.getExtras().get("data");
                    }

                    // Double-check the bitmap.
                    if (bitmap == null) {
                        Log.d(LOG_TAG, "I either have a null image path or bitmap");
                        this.failPicture("Unable to create bitmap!");
                        return;
                    }

                    if (rotate != 0 && this.correctOrientation) {
                        bitmap = getRotatedBitmap(rotate, bitmap, exif);
                    }

                    this.processPicture(bitmap);
                    checkForDuplicateImage(DATA_URL);
                }

                // If sending filename back
                else if (destType == FILE_URI || destType == NATIVE_URI) {
                    if (this.saveToPhotoAlbum) {
                        Uri inputUri = getUriFromMediaStore();
                        //Just because we have a media URI doesn't mean we have a real file, we need to make it
                        uri = Uri.fromFile(new File(FileHelper.getRealPath(inputUri, this.cordova)));
                    } else {
                        uri = Uri.fromFile(
                                new File(getTempDirectoryPath(), System.currentTimeMillis() + ".jpg"));
                    }

                    if (uri == null) {
                        this.failPicture("Error capturing image - no media storage found.");
                    }

                    // If all this is true we shouldn't compress the image.
                    if (this.targetHeight == -1 && this.targetWidth == -1 && this.mQuality == 100
                            && !this.correctOrientation) {
                        writeUncompressedImage(uri);

                        this.callbackContext.success(uri.toString());
                    } else {
                        bitmap = getScaledBitmap(FileHelper.stripFileProtocol(imageUri.toString()));

                        if (rotate != 0 && this.correctOrientation) {
                            bitmap = getRotatedBitmap(rotate, bitmap, exif);
                        }

                        // Add compressed version of captured image to returned media store Uri
                        OutputStream os = this.cordova.getActivity().getContentResolver().openOutputStream(uri);
                        bitmap.compress(Bitmap.CompressFormat.JPEG, this.mQuality, os);
                        os.close();

                        // Restore exif data to file
                        if (this.encodingType == JPEG) {
                            String exifPath;
                            if (this.saveToPhotoAlbum) {
                                exifPath = FileHelper.getRealPath(uri, this.cordova);
                            } else {
                                exifPath = uri.getPath();
                            }
                            exif.createOutFile(exifPath);
                            exif.writeExifData();
                        }

                    }
                    // Send Uri back to JavaScript for viewing image
                    this.callbackContext.success(uri.toString());
                }

                this.cleanup(FILE_URI, this.imageUri, uri, bitmap);
                bitmap = null;

            } catch (IOException e) {
                e.printStackTrace();
                this.failPicture("Error capturing image.");
            }
        }

        // If cancelled
        else if (resultCode == Activity.RESULT_CANCELED) {
            this.failPicture("Camera cancelled.");
        }

        // If something else
        else {
            this.failPicture("Did not complete!");
        }
    }

    // If retrieving photo from library
    else if ((srcType == PHOTOLIBRARY) || (srcType == SAVEDPHOTOALBUM)) {
        if (resultCode == Activity.RESULT_OK) {
            Uri uri = intent.getData();

            // If you ask for video or all media type you will automatically get back a file URI
            // and there will be no attempt to resize any returned data
            if (this.mediaType != PICTURE) {
                this.callbackContext.success(uri.toString());
            } else {
                // This is a special case to just return the path as no scaling,
                // rotating, nor compressing needs to be done
                if (this.targetHeight == -1 && this.targetWidth == -1
                        && (destType == FILE_URI || destType == NATIVE_URI) && !this.correctOrientation) {
                    this.callbackContext.success(uri.toString());
                } else {
                    String uriString = uri.toString();
                    // Get the path to the image. Makes loading so much easier.
                    String mimeType = FileHelper.getMimeType(uriString, this.cordova);
                    // If we don't have a valid image so quit.
                    if (!("image/jpeg".equalsIgnoreCase(mimeType) || "image/png".equalsIgnoreCase(mimeType))) {
                        Log.d(LOG_TAG, "I either have a null image path or bitmap");
                        this.failPicture("Unable to retrieve path to picture!");
                        return;
                    }
                    Bitmap bitmap = null;
                    try {
                        bitmap = getScaledBitmap(uriString);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    if (bitmap == null) {
                        Log.d(LOG_TAG, "I either have a null image path or bitmap");
                        this.failPicture("Unable to create bitmap!");
                        return;
                    }

                    if (this.correctOrientation) {
                        rotate = getImageOrientation(uri);
                        if (rotate != 0) {
                            //                                Matrix matrix = new Matrix();
                            //                                matrix.setRotate(rotate);
                            //                                bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
                            RotateTask r = new RotateTask(bitmap, srcType, destType, rotate, intent);
                            r.execute();
                            return;
                        }
                    }
                    returnImageToProcess(bitmap, srcType, destType, intent, rotate);
                }
            }
        } else if (resultCode == Activity.RESULT_CANCELED) {
            this.failPicture("Selection cancelled.");
        } else {
            this.failPicture("Selection did not complete!");
        }
    }
}

From source file:com.tuxpan.foregroundcameragalleryplugin.ForegroundCameraLauncher.java

License:Apache License

private void returnImageToProcess(Bitmap bitmap, int srcType, int destType, Intent intent, int rotate) {
    Uri uri = intent.getData();//from w w w .j a  va2  s.c  o m

    if (destType == DATA_URL) {
        processPicture(bitmap);
    }

    // If sending filename back
    else if (destType == FILE_URI || destType == NATIVE_URI) {
        // Do we need to scale the returned file
        if (targetHeight > 0 && targetWidth > 0) {
            try {
                // Create an ExifHelper to save the exif data that is lost during compression
                String resizePath = getTempDirectoryPath() + "/resize.jpg";
                // Some content: URIs do not map to file paths (e.g. picasa).
                String realPath = FileHelper.getRealPath(uri, cordova);
                ExifHelper exif = new ExifHelper();
                if (realPath != null && encodingType == JPEG) {
                    try {
                        exif.createInFile(realPath);
                        exif.readExifData();
                        rotate = exif.getOrientation();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

                OutputStream os = new FileOutputStream(resizePath);
                bitmap.compress(Bitmap.CompressFormat.JPEG, mQuality, os);
                os.close();

                // Restore exif data to file
                if (realPath != null && encodingType == JPEG) {
                    exif.createOutFile(resizePath);
                    exif.writeExifData();
                }

                // The resized image is cached by the app in order to get around this and not have to delete you
                // application cache I'm adding the current system time to the end of the file url.
                callbackContext.success("file://" + resizePath + "?" + System.currentTimeMillis());
            } catch (Exception e) {
                e.printStackTrace();
                failPicture("Error retrieving image.");
            }
        } else {
            callbackContext.success(uri.toString());
        }
    }
    if (bitmap != null) {
        bitmap.recycle();
        bitmap = null;
    }
    System.gc();
}

From source file:com.wezka.nativecamera.NativeCameraLauncher.java

License:Apache License

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    // If image available
    if (resultCode == Activity.RESULT_OK) {
        int rotate = 0;
        try {//from w w w  .j a  va  2 s  . co m
            // Create an ExifHelper to save the exif data that is lost
            // during compression
            ExifHelper exif = new ExifHelper();
            exif.createInFile(getTempDirectoryPath(this.cordova.getActivity().getApplicationContext()) + "/Pic-"
                    + this.date + ".jpg");
            exif.readExifData();
            rotate = exif.getOrientation();

            // Read in bitmap of captured image
            Bitmap bitmap;
            try {
                bitmap = android.provider.MediaStore.Images.Media
                        .getBitmap(this.cordova.getActivity().getContentResolver(), imageUri);
            } catch (FileNotFoundException e) {
                Uri uri = intent.getData();
                android.content.ContentResolver resolver = this.cordova.getActivity().getContentResolver();
                bitmap = android.graphics.BitmapFactory.decodeStream(resolver.openInputStream(uri));
            }

            // If bitmap cannot be decoded, this may return null
            if (bitmap == null) {
                this.failPicture("Error decoding image.");
                return;
            }

            bitmap = scaleBitmap(bitmap);

            // Add compressed version of captured image to returned media
            // store Uri
            bitmap = getRotatedBitmap(rotate, bitmap, exif);
            Log.i(LOG_TAG, "URI: " + this.imageUri.toString());
            OutputStream os = this.cordova.getActivity().getContentResolver().openOutputStream(this.imageUri);
            bitmap.compress(Bitmap.CompressFormat.JPEG, this.mQuality, os);
            os.close();

            // Restore exif data to file
            exif.createOutFile(this.imageUri.getPath());
            exif.writeExifData();

            // Send Uri back to JavaScript for viewing image
            this.callbackContext
                    .sendPluginResult(new PluginResult(PluginResult.Status.OK, this.imageUri.toString()));

            bitmap.recycle();
            bitmap = null;
            System.gc();
        } catch (IOException e) {
            e.printStackTrace();
            this.failPicture("Error capturing image.");
        }
    }

    // If cancelled
    else if (resultCode == Activity.RESULT_CANCELED) {
        this.failPicture("Camera cancelled.");
    }

    // If something else
    else {
        this.failPicture("Did not complete!");
    }
}

From source file:io.ingame.squarecamera.CameraLauncher.java

License:Apache License

/**
 * Applies all needed transformation to the image received from the camera.
 *
 * @param destType          In which form should we return the image
 * @param intent            An Intent, which can return result data to the caller (various data can be attached to Intent "extras").
 *//*from ww  w .  j a v  a  2s .  co m*/
private void processResultFromCamera(int destType, Intent intent) throws IOException {
    int rotate = 0;

    // Create an ExifHelper to save the exif data that is lost during compression
    ExifHelper exif = new ExifHelper();
    try {
        if (this.encodingType == JPEG) {
            exif.createInFile(getTempDirectoryPath() + "/.Pic.jpg");
            exif.readExifData();
            rotate = exif.getOrientation();
        } else if (this.encodingType == PNG) {
            exif.createInFile(getTempDirectoryPath() + "/.Pic.png");
            exif.readExifData();
            rotate = exif.getOrientation();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    Bitmap bitmap = null;
    Uri uri = null;

    // If sending base64 image back
    if (destType == DATA_URL) {
        bitmap = getScaledBitmap(FileHelper.stripFileProtocol(imageUri.toString()));
        if (bitmap == null) {
            // Try to get the bitmap from intent.
            bitmap = (Bitmap) intent.getExtras().get("data");
        }

        // Double-check the bitmap.
        if (bitmap == null) {
            Log.d(LOG_TAG, "I either have a null image path or bitmap");
            this.failPicture("Unable to create bitmap!");
            return;
        }

        if (rotate != 0 && this.correctOrientation) {
            bitmap = getRotatedBitmap(rotate, bitmap, exif);
        }

        this.processPicture(bitmap);
        checkForDuplicateImage(DATA_URL);
    }

    // If sending filename back
    else if (destType == FILE_URI || destType == NATIVE_URI) {
        if (this.saveToPhotoAlbum) {
            Uri inputUri = getUriFromMediaStore();
            try {
                //Just because we have a media URI doesn't mean we have a real file, we need to make it
                uri = Uri.fromFile(new File(FileHelper.getRealPath(inputUri, this.cordova)));
            } catch (NullPointerException e) {
                uri = null;
            }
        } else {
            uri = Uri.fromFile(new File(getTempDirectoryPath(), System.currentTimeMillis() + ".jpg"));
        }

        if (uri == null) {
            this.failPicture("Error capturing image - no media storage found.");
            return;
        }

        // If all this is true we shouldn't compress the image.
        if (this.targetHeight == -1 && this.targetWidth == -1 && this.mQuality == 100
                && !this.correctOrientation) {
            writeUncompressedImage(uri);

            this.callbackContext.success(uri.toString());
        } else {
            bitmap = getScaledBitmap(FileHelper.stripFileProtocol(imageUri.toString()));

            if (rotate != 0 && this.correctOrientation) {
                bitmap = getRotatedBitmap(rotate, bitmap, exif);
            }

            // Add compressed version of captured image to returned media store Uri
            OutputStream os = this.cordova.getActivity().getContentResolver().openOutputStream(uri);
            bitmap.compress(Bitmap.CompressFormat.JPEG, this.mQuality, os);
            os.close();

            // Restore exif data to file
            if (this.encodingType == JPEG) {
                String exifPath;
                if (this.saveToPhotoAlbum) {
                    exifPath = FileHelper.getRealPath(uri, this.cordova);
                } else {
                    exifPath = uri.getPath();
                }
                exif.createOutFile(exifPath);
                exif.writeExifData();
            }
            if (this.allowEdit) {
                performCrop(uri);
            } else {
                // Send Uri back to JavaScript for viewing image
                this.callbackContext.success(uri.toString());
            }
        }
    } else {
        throw new IllegalStateException();
    }

    this.cleanup(FILE_URI, this.imageUri, uri, bitmap);
    bitmap = null;
}

From source file:io.strider.camera.CameraLauncher.java

License:Apache License

/**
 * Called when the camera view exits.//from www  .j av a 2  s  .c o  m
 *
 * @param requestCode       The request code originally supplied to startActivityForResult(),
 *                          allowing you to identify who this result came from.
 * @param resultCode        The integer result code returned by the child activity through its setResult().
 * @param intent            An Intent, which can return result data to the caller (various data can be attached to Intent "extras").
 */
public void onActivityResult(int requestCode, int resultCode, Intent intent) {

    // Get src and dest types from request code
    int srcType = (requestCode / 16) - 1;
    int destType = (requestCode % 16) - 1;
    int rotate = 0;

    // If CAMERA
    if (srcType == CAMERA) {
        // If image available
        if (resultCode == Activity.RESULT_OK) {
            try {
                // Create an ExifHelper to save the exif data that is lost during compression
                ExifHelper exif = new ExifHelper();
                try {
                    if (this.encodingType == JPEG) {
                        exif.createInFile(getTempDirectoryPath() + "/.Pic.jpg");
                        exif.readExifData();
                        rotate = exif.getOrientation();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }

                Bitmap bitmap = null;
                Uri uri = null;

                // If sending base64 image back
                if (destType == DATA_URL) {
                    bitmap = getScaledBitmap(FileHelper.stripFileProtocol(imageUri.toString()));
                    if (bitmap == null) {
                        // Try to get the bitmap from intent.
                        bitmap = (Bitmap) intent.getExtras().get("data");
                    }

                    // Double-check the bitmap.
                    if (bitmap == null) {
                        Log.d(LOG_TAG, "I either have a null image path or bitmap");
                        this.failPicture("Unable to create bitmap!");
                        return;
                    }

                    if (rotate != 0 && this.correctOrientation) {
                        bitmap = getRotatedBitmap(rotate, bitmap, exif);
                    }

                    this.processPicture(bitmap);
                    checkForDuplicateImage(DATA_URL);
                }

                // If sending filename back
                else if (destType == FILE_URI || destType == NATIVE_URI) {
                    if (this.saveToPhotoAlbum) {
                        Uri inputUri = getUriFromMediaStore();
                        //Just because we have a media URI doesn't mean we have a real file, we need to make it
                        uri = Uri.fromFile(new File(FileHelper.getRealPath(inputUri, this.cordova)));
                    } else {
                        uri = Uri.fromFile(
                                new File(getTempDirectoryPath(), System.currentTimeMillis() + ".jpg"));
                    }

                    if (uri == null) {
                        this.failPicture("Error capturing image - no media storage found.");
                    }

                    // If all this is true we shouldn't compress the image.
                    if (this.targetHeight == -1 && this.targetWidth == -1 && this.mQuality == 100
                            && !this.correctOrientation) {
                        writeUncompressedImage(uri);

                        this.callbackContext.success(uri.toString());
                    } else {
                        bitmap = getScaledBitmap(FileHelper.stripFileProtocol(imageUri.toString()));

                        if (rotate != 0 && this.correctOrientation) {
                            bitmap = getRotatedBitmap(rotate, bitmap, exif);
                        }

                        // Add compressed version of captured image to returned media store Uri
                        OutputStream os = this.cordova.getActivity().getContentResolver().openOutputStream(uri);
                        bitmap.compress(Bitmap.CompressFormat.JPEG, this.mQuality, os);
                        os.close();

                        // Restore exif data to file
                        if (this.encodingType == JPEG) {
                            String exifPath;
                            if (this.saveToPhotoAlbum) {
                                exifPath = FileHelper.getRealPath(uri, this.cordova);
                            } else {
                                exifPath = uri.getPath();
                            }
                            exif.createOutFile(exifPath);
                            exif.writeExifData();
                        }

                    }
                    // Send Uri back to JavaScript for viewing image
                    this.callbackContext.success(uri.toString());
                }

                this.cleanup(FILE_URI, this.imageUri, uri, bitmap);
                bitmap = null;

            } catch (IOException e) {
                e.printStackTrace();
                this.failPicture("Error capturing image.");
            }
        }

        // If cancelled
        else if (resultCode == Activity.RESULT_CANCELED) {
            this.failPicture("Camera cancelled.");
        }

        // If something else
        else {
            this.failPicture("Did not complete!");
        }
    }

    //      // If retrieving photo from library
    //      else if ((srcType == PHOTOLIBRARY) || (srcType == SAVEDPHOTOALBUM)) {
    //         if (resultCode == Activity.RESULT_OK) {
    //            Uri uri = intent.getData();
    //
    //            // If you ask for video or all media type you will automatically get back a file URI
    //            // and there will be no attempt to resize any returned data
    //            if (this.mediaType != PICTURE) {
    //               this.callbackContext.success(uri.toString());
    //            }
    //            else {
    //               // This is a special case to just return the path as no scaling,
    //               // rotating, nor compressing needs to be done
    //               if (this.targetHeight == -1 && this.targetWidth == -1 &&
    //                     (destType == FILE_URI || destType == NATIVE_URI) && !this.correctOrientation) {
    //                  this.callbackContext.success(uri.toString());
    //               } else {
    //                  String uriString = uri.toString();
    //                  // Get the path to the image. Makes loading so much easier.
    //                  String mimeType = FileHelper.getMimeType(uriString, this.cordova);
    //                  // If we don't have a valid image so quit.
    //                  if (!("image/jpeg".equalsIgnoreCase(mimeType) || "image/png".equalsIgnoreCase(mimeType))) {
    //                     Log.d(LOG_TAG, "I either have a null image path or bitmap");
    //                     this.failPicture("Unable to retrieve path to picture!");
    //                     return;
    //                  }
    //                  Bitmap bitmap = null;
    //                  try {
    //                     bitmap = getScaledBitmap(uriString);
    //                  } catch (IOException e) {
    //                     e.printStackTrace();
    //                  }
    //                  if (bitmap == null) {
    //                     Log.d(LOG_TAG, "I either have a null image path or bitmap");
    //                     this.failPicture("Unable to create bitmap!");
    //                     return;
    //                  }
    //
    //                  if (this.correctOrientation) {
    //                     rotate = getImageOrientation(uri);
    //                     if (rotate != 0) {
    //                        Matrix matrix = new Matrix();
    //                        matrix.setRotate(rotate);
    //                        bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    //                     }
    //                  }
    //
    //                  // If sending base64 image back
    //                  if (destType == DATA_URL) {
    //                     this.processPicture(bitmap);
    //                  }
    //
    //                  // If sending filename back
    //                  else if (destType == FILE_URI || destType == NATIVE_URI) {
    //                     // Do we need to scale the returned file
    //                     if (this.targetHeight > 0 && this.targetWidth > 0) {
    //                        try {
    //                           // Create an ExifHelper to save the exif data that is lost during compression
    //                           String resizePath = getTempDirectoryPath() + "/resize.jpg";
    //                           // Some content: URIs do not map to file paths (e.g. picasa).
    //                           String realPath = FileHelper.getRealPath(uri, this.cordova);
    //                           ExifHelper exif = new ExifHelper();
    //                           if (realPath != null && this.encodingType == JPEG) {
    //                              try {
    //                                 exif.createInFile(realPath);
    //                                 exif.readExifData();
    //                                 rotate = exif.getOrientation();
    //                              } catch (IOException e) {
    //                                 e.printStackTrace();
    //                              }
    //                           }
    //
    //                           OutputStream os = new FileOutputStream(resizePath);
    //                           bitmap.compress(Bitmap.CompressFormat.JPEG, this.mQuality, os);
    //                           os.close();
    //
    //                           // Restore exif data to file
    //                           if (realPath != null && this.encodingType == JPEG) {
    //                              exif.createOutFile(resizePath);
    //                              exif.writeExifData();
    //                           }
    //
    //                           // The resized image is cached by the app in order to get around this and not have to delete you
    //                           // application cache I'm adding the current system time to the end of the file url.
    //                           this.callbackContext.success("file://" + resizePath + "?" + System.currentTimeMillis());
    //                        } catch (Exception e) {
    //                           e.printStackTrace();
    //                           this.failPicture("Error retrieving image.");
    //                        }
    //                     }
    //                     else {
    //                        this.callbackContext.success(uri.toString());
    //                     }
    //                  }
    //                  if (bitmap != null) {
    //                     bitmap.recycle();
    //                     bitmap = null;
    //                  }
    //                  System.gc();
    //               }
    //            }
    //         }
    //         else if (resultCode == Activity.RESULT_CANCELED) {
    //            this.failPicture("Selection cancelled.");
    //         }
    //         else {
    //            this.failPicture("Selection did not complete!");
    //         }
    //      }
}

From source file:pro.alyans.nativecamera.NativeCameraLauncher.java

License:Apache License

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    // If image available
    if (resultCode == Activity.RESULT_OK) {
        if (this.srcType == CAMERA) {
            int rotate = 0;
            try {
                // Create an ExifHelper to save the exif data that is lost
                // during compression
                ExifHelper exif = new ExifHelper();
                exif.createInFile(getTempDirectoryPath(this.cordova.getActivity().getApplicationContext())
                        + "/Pic-" + this.date + ".jpg");
                exif.readExifData();/*  w w w. j  a  va  2s  .c om*/
                rotate = exif.getOrientation();

                // Read in bitmap of captured image
                Bitmap bitmap;
                try {
                    bitmap = android.provider.MediaStore.Images.Media
                            .getBitmap(this.cordova.getActivity().getContentResolver(), imageUri);
                } catch (FileNotFoundException e) {
                    Uri uri = intent.getData();
                    android.content.ContentResolver resolver = this.cordova.getActivity().getContentResolver();
                    bitmap = android.graphics.BitmapFactory.decodeStream(resolver.openInputStream(uri));
                }

                // If bitmap cannot be decoded, this may return null
                if (bitmap == null) {
                    this.failPicture("Error decoding image.");
                    return;
                }

                bitmap = scaleBitmap(bitmap);

                // Add compressed version of captured image to returned media
                // store Uri
                bitmap = getRotatedBitmap(rotate, bitmap, exif);
                Log.i(LOG_TAG, "URI: " + this.imageUri.toString());
                OutputStream os = this.cordova.getActivity().getContentResolver()
                        .openOutputStream(this.imageUri);
                bitmap.compress(Bitmap.CompressFormat.JPEG, this.mQuality, os);
                os.close();

                // Restore exif data to file
                exif.createOutFile(this.imageUri.getPath());
                exif.writeExifData();

                // Send Uri back to JavaScript for viewing image
                this.callbackContext
                        .sendPluginResult(new PluginResult(PluginResult.Status.OK, this.imageUri.toString()));

                bitmap.recycle();
                bitmap = null;
                System.gc();
            } catch (IOException e) {
                e.printStackTrace();
                this.failPicture("Error capturing image.");
            }
        } else if ((this.srcType == PHOTOLIBRARY) || (this.srcType == SAVEDPHOTOALBUM)) {
            if (resultCode == Activity.RESULT_OK && intent != null) {
                final Intent i = intent;
                final int finalDestType = destType;
                cordova.getThreadPool().execute(new Runnable() {
                    public void run() {
                        processResultFromGallery(finalDestType, i);
                    }
                });
            } else if (resultCode == Activity.RESULT_CANCELED) {
                this.failPicture("No Image Selected");
            } else {
                this.failPicture("Selection did not complete!");
            }
        }
    }

    // If cancelled
    else if (resultCode == Activity.RESULT_CANCELED) {
        this.failPicture("Camera cancelled.");
    }

    // If something else
    else {
        this.failPicture("Did not complete!");
    }
}