Example usage for android.graphics Matrix setRotate

List of usage examples for android.graphics Matrix setRotate

Introduction

In this page you can find the example usage for android.graphics Matrix setRotate.

Prototype

public void setRotate(float degrees) 

Source Link

Document

Set the matrix to rotate about (0,0) by the specified number of degrees.

Usage

From source file:com.cordova.photo.CameraLauncher.java

/**
 * Figure out if the bitmap should be rotated. For instance if the picture was taken in
 * portrait mode/*from  w w  w  .jav  a2 s  .  c  o  m*/
 *
 * @param rotate
 * @param bitmap
 * @return rotated bitmap
 */
private Bitmap getRotatedBitmap(int rotate, Bitmap bitmap, ExifHelper exif) {
    Matrix matrix = new Matrix();
    if (rotate == 180) {
        matrix.setRotate(rotate);
    } else {
        matrix.setRotate(rotate, (float) bitmap.getWidth() / 2, (float) bitmap.getHeight() / 2);
    }

    try {
        bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        exif.resetOrientation();
    } catch (OutOfMemoryError oom) {
        // You can run out of memory if the image is very large:
        // http://simonmacdonald.blogspot.ca/2012/07/change-to-camera-code-in-phonegap-190.html
        // If this happens, simply do not rotate the image and return it unmodified.
        // If you do not catch the OutOfMemoryError, the Android app crashes.
    }

    return bitmap;
}

From source file:org.apache.cordova.core.CameraLauncher.java

/**
 * Called when the camera view exits./*from  w  ww . ja  va2 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(DirectoryManager.getTempDirectoryPath(this.cordova.getActivity())
                                + "/.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(DirectoryManager.getTempDirectoryPath(this.cordova.getActivity()),
                                        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 = DirectoryManager
                                        .getTempDirectoryPath(this.cordova.getActivity()) + "/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:org.apache.cordova.CameraLauncher.java

/**
 * Called when the camera view exits.// www .  j a v  a  2  s .com
 *
 * @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:com.cordova.photo.CameraLauncher.java

/**
     * Applies all needed transformation to the image received from the gallery.
     */* w  w w. jav a2  s  .  c  om*/
     * @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").
     */
private void processResultFromGallery(int destType, Intent intent) {
    Uri uri = intent.getData();
    if (uri == null) {
        if (croppedUri != null) {
            uri = croppedUri;
        } else {
            this.failPicture("null data from photo library");
            return;
        }
    }
    int rotate = 0;

    // 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.activity);
            // 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);
                    try {
                        bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(),
                                matrix, true);
                        this.orientationCorrected = true;
                    } catch (OutOfMemoryError oom) {
                        this.orientationCorrected = false;
                    }
                }
            }

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

            // If sending filename back
            else if (destType == FILE_URI || destType == NATIVE_URI) {
                // Did we modify the image?
                if ((this.targetHeight > 0 && this.targetWidth > 0)
                        || (this.correctOrientation && this.orientationCorrected)) {
                    try {
                        String modifiedPath = this.ouputModifiedBitmap(bitmap, uri);
                        // The modified 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://" + modifiedPath + "?" + 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();
        }
    }
}

From source file:com.remobile.camera.CameraLauncher.java

/**
 * Applies all needed transformation to the image received from the gallery.
 *
 * @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 a  2s  .  co m
private void processResultFromGallery(int destType, Intent intent) {
    Uri uri = intent.getData();
    if (uri == null) {
        if (croppedUri != null) {
            uri = croppedUri;
        } else {
            this.failPicture("null data from photo library");
            return;
        }
    }
    int rotate = 0;

    // 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);
                    try {
                        bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(),
                                matrix, true);
                        this.orientationCorrected = true;
                    } catch (OutOfMemoryError oom) {
                        this.orientationCorrected = false;
                    }
                }
            }

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

            // If sending filename back
            else if (destType == FILE_URI || destType == NATIVE_URI) {
                // Did we modify the image?
                if ((this.targetHeight > 0 && this.targetWidth > 0)
                        || (this.correctOrientation && this.orientationCorrected)) {
                    try {
                        String modifiedPath = this.ouputModifiedBitmap(bitmap, uri);
                        // The modified 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://" + modifiedPath + "?" + 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();
        }
    }
}

From source file:com.example.camera2apidemo.Camera2BasicFragment.java

private void picture_preview(ImageView iv_show) {
    try {//w w  w.j  av a 2 s  .co m
        FileInputStream fis = new FileInputStream(mFile.getAbsolutePath());
        Bitmap bitmap = BitmapFactory.decodeStream(fis);
        Matrix matrix = new Matrix();
        matrix.setRotate(90);
        bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        iv_show.setImageBitmap(bitmap);

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

}

From source file:com.phonegap.plugins.wsiCameraLauncher.WsiCameraLauncher.java

/**
 * Called when the camera view exits.//  w  w w .jav  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;

    Log.d(LOG_TAG, "-z");

    // If retrieving photo from library
    if ((srcType == PHOTOLIBRARY) || (srcType == SAVEDPHOTOALBUM)) {
        Log.d(LOG_TAG, "-y");
        if (resultCode == Activity.RESULT_OK) {
            Log.d(LOG_TAG, "-x");
            Uri uri = intent.getData();
            Log.d(LOG_TAG, "-w");
            // 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) {
                Log.d(LOG_TAG, "mediaType not PICTURE, so must be Video");

                String metadataDateTime = "";
                ExifInterface exif;
                try {
                    exif = new ExifInterface(this.getRealPathFromURI(uri, this.cordova));
                    if (exif.getAttribute(ExifInterface.TAG_DATETIME) != null) {
                        Log.d(LOG_TAG, "z4a");
                        metadataDateTime = exif.getAttribute(ExifInterface.TAG_DATETIME).toString();
                        metadataDateTime = metadataDateTime.replaceFirst(":", "-");
                        metadataDateTime = metadataDateTime.replaceFirst(":", "-");
                    }
                } catch (IOException e2) {
                    // TODO Auto-generated catch block
                    e2.printStackTrace();
                }

                Log.d(LOG_TAG, "before create thumbnail");
                Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(
                        (new File(this.getRealPathFromURI(uri, this.cordova))).getAbsolutePath(),
                        MediaStore.Images.Thumbnails.MINI_KIND);
                Log.d(LOG_TAG, "after create thumbnail");
                String mid = generateRandomMid();

                try {
                    String filePathMedium = this.getTempDirectoryPath(this.cordova.getActivity()) + "/medium_"
                            + mid + ".jpg";
                    FileOutputStream foMedium = new FileOutputStream(filePathMedium);
                    bitmap.compress(CompressFormat.JPEG, 100, foMedium);
                    foMedium.flush();
                    foMedium.close();

                    bitmap.recycle();
                    System.gc();

                    JSONObject mediaFile = new JSONObject();
                    try {
                        mediaFile.put("mid", mid);
                        mediaFile.put("mediaType", "video");
                        mediaFile.put("filePath", filePathMedium);
                        mediaFile.put("filePathMedium", filePathMedium);
                        mediaFile.put("filePathThumb", filePathMedium);
                        mediaFile.put("typeOfPluginResult", "initialRecordInformer");
                        String absolutePath = (new File(this.getRealPathFromURI(uri, this.cordova)))
                                .getAbsolutePath();
                        mediaFile.put("fileExt", absolutePath.substring(absolutePath.lastIndexOf(".") + 1));
                        if (metadataDateTime != "") {
                            mediaFile.put("metadataDateTime", metadataDateTime);
                        }
                    } catch (JSONException e) {
                        Log.d(LOG_TAG, "error: " + e.getStackTrace().toString());
                    }
                    Log.d(LOG_TAG, "mediafile at 638" + mediaFile.toString());
                    PluginResult pluginResult = new PluginResult(PluginResult.Status.OK,
                            (new JSONArray()).put(mediaFile));
                    pluginResult.setKeepCallback(true);
                    this.callbackContext.sendPluginResult(pluginResult);
                    new UploadVideoToS3Task().execute(new File(this.getRealPathFromURI(uri, this.cordova)),
                            this.callbackContext, mid, mediaFile);
                } catch (FileNotFoundException e1) {
                    e1.printStackTrace();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            } else {
                String imagePath = this.getRealPathFromURI(uri, this.cordova);
                String mimeType = FileUtils.getMimeType(imagePath);
                // If we don't have a valid image so quit.
                if (imagePath == null || mimeType == null || !(mimeType.equalsIgnoreCase("image/jpeg")
                        || mimeType.equalsIgnoreCase("image/png"))) {
                    Log.d(LOG_TAG, "I either have a null image path or bitmap");
                    this.failPicture("Unable to retrieve path to picture!");
                    return;
                }

                String mid = generateRandomMid();

                Log.d(LOG_TAG, "a");

                JSONObject mediaFile = new JSONObject();

                Log.d(LOG_TAG, "b");

                try {
                    FileInputStream fi = new FileInputStream(imagePath);
                    Bitmap bitmap = BitmapFactory.decodeStream(fi);
                    fi.close();

                    Log.d(LOG_TAG, "z1");

                    // try to get exif data
                    ExifInterface exif = new ExifInterface(imagePath);

                    Log.d(LOG_TAG, "z2");

                    JSONObject metadataJson = new JSONObject();

                    Log.d(LOG_TAG, "z3");

                    /*
                    JSONObject latlng = new JSONObject();
                    String lat = "0";
                    String lng = "0";
                    if (exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE) != null) {
                       lat = exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE);
                    }
                    if (exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE) != null) {
                       lng = exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE);
                    }
                    latlng.put("lat", lat);
                    latlng.put("lng", lng);
                    Log.d(LOG_TAG, "z4");
                    metadataJson.put("locationData", latlng);
                    */

                    String metadataDateTime = "";

                    if (exif.getAttribute(ExifInterface.TAG_DATETIME) != null) {
                        Log.d(LOG_TAG, "z4a");
                        JSONObject exifWrapper = new JSONObject();
                        exifWrapper.put("DateTimeOriginal",
                                exif.getAttribute(ExifInterface.TAG_DATETIME).toString());
                        exifWrapper.put("DateTimeDigitized",
                                exif.getAttribute(ExifInterface.TAG_DATETIME).toString());
                        metadataDateTime = exif.getAttribute(ExifInterface.TAG_DATETIME).toString();
                        metadataDateTime = metadataDateTime.replaceFirst(":", "-");
                        metadataDateTime = metadataDateTime.replaceFirst(":", "-");
                        Log.d(LOG_TAG, "z5");
                        metadataJson.put("Exif", exifWrapper);
                    }
                    Log.d(LOG_TAG, "z6");

                    Log.d(LOG_TAG, "metadataJson: " + metadataJson.toString());
                    Log.d(LOG_TAG, "metadataDateTime: " + metadataDateTime.toString());

                    if (exif.getAttribute(ExifInterface.TAG_ORIENTATION) != null) {
                        int o = Integer.parseInt(exif.getAttribute(ExifInterface.TAG_ORIENTATION));

                        Log.d(LOG_TAG, "z7");

                        if (o == ExifInterface.ORIENTATION_NORMAL) {
                            rotate = 0;
                        } else if (o == ExifInterface.ORIENTATION_ROTATE_90) {
                            rotate = 90;
                        } else if (o == ExifInterface.ORIENTATION_ROTATE_180) {
                            rotate = 180;
                        } else if (o == ExifInterface.ORIENTATION_ROTATE_270) {
                            rotate = 270;
                        } else {
                            rotate = 0;
                        }

                        Log.d(LOG_TAG, "z8");

                        Log.d(LOG_TAG, "rotate: " + rotate);

                        // try to correct orientation
                        if (rotate != 0) {
                            Matrix matrix = new Matrix();
                            Log.d(LOG_TAG, "z9");
                            matrix.setRotate(rotate);
                            Log.d(LOG_TAG, "z10");
                            bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(),
                                    matrix, true);
                            Log.d(LOG_TAG, "z11");
                        }
                    }

                    Log.d(LOG_TAG, "c");

                    String filePath = this.getTempDirectoryPath(this.cordova.getActivity()) + "/econ_" + mid
                            + ".jpg";
                    FileOutputStream foEcon = new FileOutputStream(filePath);
                    fitInsideSquare(bitmap, 850).compress(CompressFormat.JPEG, 45, foEcon);
                    foEcon.flush();
                    foEcon.close();

                    Log.d(LOG_TAG, "d");

                    String filePathMedium = this.getTempDirectoryPath(this.cordova.getActivity()) + "/medium_"
                            + mid + ".jpg";
                    FileOutputStream foMedium = new FileOutputStream(filePathMedium);
                    makeInsideSquare(bitmap, 320).compress(CompressFormat.JPEG, 55, foMedium);
                    foMedium.flush();
                    foMedium.close();

                    Log.d(LOG_TAG, "e");

                    String filePathThumb = this.getTempDirectoryPath(this.cordova.getActivity()) + "/thumb_"
                            + mid + ".jpg";
                    FileOutputStream foThumb = new FileOutputStream(filePathThumb);
                    makeInsideSquare(bitmap, 175).compress(CompressFormat.JPEG, 55, foThumb);
                    foThumb.flush();
                    foThumb.close();

                    bitmap.recycle();
                    System.gc();

                    Log.d(LOG_TAG, "f");

                    mediaFile.put("mid", mid);
                    mediaFile.put("mediaType", "photo");
                    mediaFile.put("filePath", filePath);
                    mediaFile.put("filePathMedium", filePath);
                    mediaFile.put("filePathThumb", filePath);
                    mediaFile.put("typeOfPluginResult", "initialRecordInformer");
                    //mediaFile.put("metadataJson", metadataJson);
                    if (metadataDateTime != "") {
                        mediaFile.put("metadataDateTime", metadataDateTime);
                    }

                    PluginResult pluginResult = new PluginResult(PluginResult.Status.OK,
                            (new JSONArray()).put(mediaFile));
                    pluginResult.setKeepCallback(true);
                    this.callbackContext.sendPluginResult(pluginResult);
                    Log.d(LOG_TAG, "g");
                    Log.d(LOG_TAG, "mediaFile " + mediaFile.toString());
                    new UploadFilesToS3Task().execute(new File(filePath), new File(filePathMedium),
                            new File(filePathThumb), this.callbackContext, mid, mediaFile);
                    Log.d(LOG_TAG, "h");
                } catch (FileNotFoundException e) {
                    Log.d(LOG_TAG, "error: " + e.getStackTrace().toString());
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    Log.d(LOG_TAG, "error: " + e1.getStackTrace().toString());
                } catch (JSONException e2) {
                    // TODO Auto-generated catch block
                    Log.d(LOG_TAG, "error: " + e2.getStackTrace().toString());
                }

                /*
                if (this.correctOrientation) {
                   String[] cols = { MediaStore.Images.Media.ORIENTATION };
                   Cursor cursor = this.cordova
                .getActivity()
                .getContentResolver()
                .query(intent.getData(), cols, null, null,
                      null);
                   if (cursor != null) {
                      cursor.moveToPosition(0);
                      rotate = cursor.getInt(0);
                      cursor.close();
                   }
                   if (rotate != 0) {
                      Matrix matrix = new Matrix();
                      matrix.setRotate(rotate);
                      bitmap = Bitmap.createBitmap(bitmap, 0, 0,
                   bitmap.getWidth(), bitmap.getHeight(),
                   matrix, true);
                   }
                }
                        
                // Create an ExifHelper to save the exif
                // data that is lost during compression
                String resizePath = this
                      .getTempDirectoryPath(this.cordova
                   .getActivity())
                      + "/resize.jpg";
                ExifHelper exif = new ExifHelper();
                try {
                   if (this.encodingType == JPEG) {
                      exif.createInFile(resizePath);
                      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 (this.encodingType == JPEG) {
                   exif.createOutFile(this
                .getRealPathFromURI(uri,
                      this.cordova));
                   exif.writeExifData();
                }
                        
                if (bitmap != null) {
                   bitmap.recycle();
                   bitmap = null;
                }
                System.gc();
                        
                // The resized image is cached by the app in
                // order to get around this and not have to
                // delete your
                // application cache I'm adding the current
                // system time to the end of the file url.
                this.callbackContext.success("file://" + resizePath + "?" + System.currentTimeMillis());
                */
            }
        } else if (resultCode == Activity.RESULT_CANCELED) {
            this.failPicture("Selection cancelled.");
        } else {
            this.failPicture("Selection did not complete!");
        }
    }
}