Example usage for org.apache.cordova ExifHelper readExifData

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

Introduction

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

Prototype

public void readExifData() 

Source Link

Document

Reads all the EXIF data from the input file.

Usage

From source file:co.mwater.foregroundcameraplugin.ForegroundCameraLauncher.java

License:Apache License

/**
 * Called when the camera view exits./*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) {

    // 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();
            exif.createInFile(
                    getTempDirectoryPath(this.cordova.getActivity().getApplicationContext()) + "/Pic.jpg");
            exif.readExifData();

            // 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));
            }

            bitmap = scaleBitmap(bitmap);

            // Create entry in media store for image
            // (Don't use insertImage() because it uses default compression
            // setting of 50 - no way to change it)
            ContentValues values = new ContentValues();
            values.put(android.provider.MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
            Uri uri = null;
            try {
                uri = this.cordova.getActivity().getContentResolver()
                        .insert(android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
            } catch (UnsupportedOperationException e) {
                LOG.d(LOG_TAG, "Can't write to external media storage.");
                try {
                    uri = this.cordova.getActivity().getContentResolver()
                            .insert(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI, values);
                } catch (UnsupportedOperationException ex) {
                    LOG.d(LOG_TAG, "Can't write to internal media storage.");
                    this.failPicture("Error capturing image - no media storage found.");
                    return;
                }
            }

            // 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
            exif.createOutFile(getRealPathFromURI(uri, this.cordova));
            exif.writeExifData();

            android.util.Log.i("CameraPlugin", "destinationType: " + this.destinationType);

            if (this.destinationType == 1) { //File URI
                // Send Uri back to JavaScript for viewing image
                this.callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, uri.toString()));
            } else if (this.destinationType == 0) { //DATA URL
                String base64Data = ForegroundCameraLauncher.encodeTobase64(bitmap, this.mQuality);
                this.callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, base64Data));
            } else {
                this.failPicture("Unsupported destination");
            }

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

            checkForDuplicateImage();
        } 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:com.cloudstudio.camera.ForegroundCameraLauncher.java

License:Apache License

/**
 * Called when the camera view exits.//from  ww w  .j  a  va2s.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) {

    // 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();
            exif.createInFile(
                    getTempDirectoryPath(this.cordova.getActivity().getApplicationContext()) + "/Pic.jpg");
            exif.readExifData();

            // 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));
            }

            bitmap = scaleBitmap(bitmap);

            // Create entry in media store for image
            // (Don't use insertImage() because it uses default compression
            // setting of 50 - no way to change it)
            ContentValues values = new ContentValues();
            values.put(android.provider.MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
            Uri uri = null;
            try {
                Log.d("camera", "external_content_uri:"
                        + android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                uri = this.cordova.getActivity().getContentResolver()
                        .insert(android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
            } catch (UnsupportedOperationException e) {
                LOG.d(LOG_TAG, "Can't write to external media storage.");
                try {
                    uri = this.cordova.getActivity().getContentResolver()
                            .insert(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI, values);
                } catch (UnsupportedOperationException ex) {
                    LOG.d(LOG_TAG, "Can't write to internal media storage.");
                    this.failPicture("Error capturing image - no media storage found.");
                    return;
                }
            }
            Log.d("camera", "uri:" + uri.toString());
            // 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
            exif.createOutFile(getRealPathFromURI(uri, this.cordova));
            exif.writeExifData();

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

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

            checkForDuplicateImage();
        } 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: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").
 *///from ww  w. ja  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.cooee.cordova.plugins.camera.CameraLauncher.java

License:Apache License

private String ouputModifiedBitmap(Bitmap bitmap, Uri uri) throws IOException {
    // Create an ExifHelper to save the exif data that is lost during compression
    String modifiedPath = getTempDirectoryPath() + "/modified.jpg";

    OutputStream os = new FileOutputStream(modifiedPath);
    bitmap.compress(Bitmap.CompressFormat.JPEG, this.mQuality, os);
    os.close();//from  w  w w  .j a  v a2 s .  com

    // 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();
            if (this.correctOrientation && this.orientationCorrected) {
                exif.resetOrientation();
            }
            exif.createOutFile(modifiedPath);
            exif.writeExifData();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return modifiedPath;
}

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

License:Apache License

/**
 * Called when the camera view exits.//from  w w w . ja v  a 2  s .  c om
 *
 * @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.foregroundcameraplugin.ForegroundCameraLauncher.java

License:Apache License

/**
 * Called when the camera view exits./*  www. j a  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) {

    // 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();
            exif.createInFile(
                    getTempDirectoryPath(this.cordova.getActivity().getApplicationContext()) + "/Pic.jpg");
            exif.readExifData();

            // 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));
            }

            bitmap = scaleBitmap(bitmap);

            // Create entry in media store for image
            // (Don't use insertImage() because it uses default compression
            // setting of 50 - no way to change it)
            ContentValues values = new ContentValues();
            values.put(android.provider.MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
            Uri uri = null;
            try {
                uri = this.cordova.getActivity().getContentResolver()
                        .insert(android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
            } catch (UnsupportedOperationException e) {
                LOG.d(LOG_TAG, "Can't write to external media storage.");
                try {
                    uri = this.cordova.getActivity().getContentResolver()
                            .insert(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI, values);
                } catch (UnsupportedOperationException ex) {
                    LOG.d(LOG_TAG, "Can't write to internal media storage.");
                    this.failPicture("Error capturing image - no media storage found.");
                    return;
                }
            }

            // 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
            exif.createOutFile(getRealPathFromURI(uri, this.ctx));
            exif.writeExifData();

            // Send Uri back to JavaScript for viewing image
            this.success(new PluginResult(PluginResult.Status.OK, getRealPathFromURI(uri, this.ctx)),
                    this.callbackId);

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

            checkForDuplicateImage();
        } 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: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   ww  w .ja  va2  s  .  c om
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").
 *//*www  .  ja  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();
    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.magictools.phonegap.demos.ForegroundCameraLauncher.java

License:Apache License

/**
 * Called when the camera view exits./*from  ww w .  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)
//--------------------------------
{
    // 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();
            exif.createInFile(
                    getTempDirectoryPath(this.cordova.getActivity().getApplicationContext()) + "/Pic.jpg");
            exif.readExifData();

            // 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));
            }

            bitmap = scaleBitmap(bitmap);

            // Create entry in media store for image
            // (Don't use insertImage() because it uses default compression
            // setting of 50 - no way to change it)
            ContentValues values = new ContentValues();
            values.put(android.provider.MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
            Uri uri = null;
            try {
                uri = this.cordova.getActivity().getContentResolver()
                        .insert(android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
            } catch (UnsupportedOperationException e) {
                LOG.d(LOG_TAG, "Can't write to external media storage.");

                try {
                    uri = this.cordova.getActivity().getContentResolver()
                            .insert(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI, values);
                } catch (UnsupportedOperationException ex) {
                    LOG.d(LOG_TAG, "Can't write to internal media storage.");
                    this.failPicture("Error capturing image - no media storage found.");
                    return;
                }
            }

            // 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
            exif.createOutFile(getRealPathFromURI(uri, this.cordova));
            exif.writeExifData();

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

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

            checkForDuplicateImage();
        } 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:com.phonegap.customcamera.NativeCameraLauncher.java

License:Apache License

@Override
public void run() {

    WindowManager windowManager = (WindowManager) this.cordova.getActivity().getApplicationContext()
            .getSystemService(this.cordova.getActivity().getApplicationContext().WINDOW_SERVICE);
    int rotation = windowManager.getDefaultDisplay().getRotation();

    // Read in bitmap of captured image
    Bitmap bitmap;//from   ww w  .  j a  v a 2  s . com
    ExifHelper exif = new ExifHelper();

    //Log.i("orientation", rotation+" rotation");
    int rotate = rotation;
    try {
        // Create an ExifHelper to save the exif data that is lost
        // during compression

        exif.createInFile(getTempDirectoryPath(this.cordova.getActivity().getApplicationContext()) + "/Pic-"
                + this.date + ".jpg");
        exif.readExifData();
        /*Auskommentiert weil es immer auf Querformat gedreht hat*/
        //  rotate = exif.getOrientation();
        Log.i("orientation", rotate + " ");
        //  rotate = 90;

        try {
            bitmap = android.provider.MediaStore.Images.Media
                    .getBitmap(this.cordova.getActivity().getContentResolver(), imageUri);
            Log.i("orientation", bitmap.getWidth() + " " + bitmap.getHeight());
            if (bitmap.getWidth() > bitmap.getHeight()) {
                rotate = rotate + 90;
            }
        } catch (FileNotFoundException e) {
            Uri uri = resultIntent.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);
        // ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        // bitmap.compress(CompressFormat.JPEG, this.mQuality, outStream);
        bitmap.compress(Bitmap.CompressFormat.JPEG, this.mQuality, os);
        // String imgString = Base64.encodeToString(outStream.toByteArray(), Base64.NO_WRAP);

        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.getPath()));
        // this.callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, imgString));

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

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

}