Android Open Source - bv-android-sdk Camera Utils






From Project

Back to project page bv-android-sdk.

License

The source code is released under:

Apache License

If you think the Android project bv-android-sdk listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.bazaarvoice.intentexample;
//from   ww w  .j  ava2 s.com
import java.io.IOException;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.net.Uri;
import android.provider.MediaStore;
import android.provider.MediaStore.Images;
import android.util.Log;

/**
 * CameraUtils.java <br>
 * ReviewSubmissionExample<br>
 * 
 * A few utilites that ease the process of saving a captured image to file and
 * retrieving it for viewing and upload.
 * 
 * <p>
 * This class aids in importing photos as well sized Bitmaps as per <a
 * href="http://developer.android.com/training/displaying-bitmaps/index.html"
 * >Displaying Bitmaps Efficiently</a>.
 * 
 * <p>
 * Created on 6/29/12. Copyright (c) 2012 BazaarVoice. All rights reserved.
 * 
 * @author Bazaarvoice Engineering
 */
public class CameraUtils {

  private final static String TAG = "CameraUtils";

  /**
   * Pulls a bitmap out of the given Uri and does any necessary rotations to
   * make it suitable for display.
   * 
   * @param imageUri
   *            the Uri pointing to the image
   * @param context
   *            the applications's context
   * @param reqHeight
   * @param reqWidth
   * @return a correctly oriented Bitmap
   * @throws IOException
   */
  public static Bitmap getOrientedBitmap(Uri imageUri, Context context,
      int reqHeight, int reqWidth) throws IOException {

    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 1;

    // Decode bitmap with inSampleSize set
    Bitmap photo = BitmapFactory.decodeFile(
        getRealPathFromURI(imageUri, context), options);

    Matrix matrix = new Matrix();
    float rotation = CameraUtils.rotationForImage(context, imageUri);
    if (rotation != 0f) {
      matrix.preRotate(rotation);
    }
    Log.i("Scaled",
        "Height = " + photo.getHeight() + " Width = "
            + photo.getWidth());
    return Bitmap.createBitmap(photo, 0, 0, photo.getWidth(),
        photo.getHeight(), matrix, true);
  }

  /**
   * Helper method to reslove a uri into a path.
   * 
   * @param contentURI
   *            a uri path
   * @return the path as a string
   */
  public static String getRealPathFromURI(Uri contentURI, Context context) {
    Cursor cursor = context.getContentResolver().query(contentURI, null,
        null, null, null);
    cursor.moveToFirst();
    int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
    String path = cursor.getString(idx);
    cursor.close();
    return path;
  }

  /**
   * Calculates the amount of rotation needed for the image to look upright.
   * 
   * @param context
   *            the application's context
   * @param uri
   *            the Uri pointing to the file
   * @return the needed rotation as a <code>float</code>
   */
  private static float rotationForImage(Context context, Uri uri) {
    if ("content".equals(uri.getScheme())) {
      String[] projection = { Images.ImageColumns.ORIENTATION };
      Cursor c = context.getContentResolver().query(uri, projection,
          null, null, null);
      if (c.moveToFirst()) {
        return c.getInt(0);
      }
    } else if ("file".equals(uri.getScheme())) {
      try {
        ExifInterface exif = new ExifInterface(uri.getPath());
        int rotation = (int) exifOrientationToDegrees(exif
            .getAttributeInt(ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_NORMAL));
        return rotation;
      } catch (IOException e) {
        Log.e(TAG, "Error checking exif", e);
      }
    }
    return 0f;
  }

  /**
   * Converts the given Exif Orientation to a degree for rotation.
   * 
   * @param exifOrientation
   *            the ExifInterface code for the orientation
   * @return the rotation as a <code>float</code>
   */
  private static float exifOrientationToDegrees(int exifOrientation) {
    if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) {
      return 90;
    } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) {
      return 180;
    } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) {
      return 270;
    }
    return 0;
  }

}




Java Source Code List

com.bazaarvoice.BazaarException.java
com.bazaarvoice.BazaarParams.java
com.bazaarvoice.BazaarRequest.java
com.bazaarvoice.DisplayParams.java
com.bazaarvoice.Media.java
com.bazaarvoice.OnBazaarResponse.java
com.bazaarvoice.SubmissionMediaParams.java
com.bazaarvoice.SubmissionParams.java
com.bazaarvoice.example.browseproducts.BazaarFunctions.java
com.bazaarvoice.example.browseproducts.BazaarProduct.java
com.bazaarvoice.example.browseproducts.BazaarReview.java
com.bazaarvoice.example.browseproducts.BazaarUIThreadResponse.java
com.bazaarvoice.example.browseproducts.ImageDownloader.java
com.bazaarvoice.example.browseproducts.MainActivity.java
com.bazaarvoice.example.browseproducts.OnImageDownloadComplete.java
com.bazaarvoice.example.browseproducts.ProductAdapter.java
com.bazaarvoice.example.browseproducts.ProductsActivity.java
com.bazaarvoice.example.browseproducts.ReviewAdapter.java
com.bazaarvoice.example.browseproducts.ReviewDisplayActivity.java
com.bazaarvoice.example.browseproducts.ReviewsActivity.java
com.bazaarvoice.example.reviewsubmission.BazaarFunctions.java
com.bazaarvoice.example.reviewsubmission.BazaarReview.java
com.bazaarvoice.example.reviewsubmission.CameraUtils.java
com.bazaarvoice.example.reviewsubmission.ImageDownloader.java
com.bazaarvoice.example.reviewsubmission.MainActivity.java
com.bazaarvoice.example.reviewsubmission.OnImageDownloadComplete.java
com.bazaarvoice.example.reviewsubmission.OnImageUploadComplete.java
com.bazaarvoice.example.reviewsubmission.RatingActivity.java
com.bazaarvoice.example.reviewsubmission.RatingPreviewActivity.java
com.bazaarvoice.example.reviewsubmission.ReviewSubmissionApp.java
com.bazaarvoice.intentexample.BazaarFunctions.java
com.bazaarvoice.intentexample.BazaarUIThreadResponse.java
com.bazaarvoice.intentexample.CameraUtils.java
com.bazaarvoice.intentexample.MainActivity.java
com.bazaarvoice.types.Action.java
com.bazaarvoice.types.ApiVersion.java
com.bazaarvoice.types.Equality.java
com.bazaarvoice.types.FeedbackContentType.java
com.bazaarvoice.types.FeedbackType.java
com.bazaarvoice.types.FeedbackVoteType.java
com.bazaarvoice.types.IncludeStatsType.java
com.bazaarvoice.types.IncludeType.java
com.bazaarvoice.types.MediaParamsContentType.java
com.bazaarvoice.types.RequestType.java
com.chute.android.photopickerplus.adapter.AlbumsAdapter.java
com.chute.android.photopickerplus.adapter.PhotoSelectCursorAdapter.java
com.chute.android.photopickerplus.adapter.PhotosAdapter.java
com.chute.android.photopickerplus.app.AlbumsActivity.java
com.chute.android.photopickerplus.app.ChooseServiceActivity.java
com.chute.android.photopickerplus.app.GridActivity.java
com.chute.android.photopickerplus.app.PhotoPickerPlusApp.java
com.chute.android.photopickerplus.dao.MediaDAO.java
com.chute.android.photopickerplus.util.AppUtil.java
com.chute.android.photopickerplus.util.Constants.java
com.chute.android.photopickerplus.util.NotificationUtil.java
com.chute.android.photopickerplus.util.intent.AlbumsActivityIntentWrapper.java
com.chute.android.photopickerplus.util.intent.IntentUtil.java
com.chute.android.photopickerplus.util.intent.IntentWrapper.java
com.chute.android.photopickerplus.util.intent.PhotoActivityIntentWrapper.java
com.chute.android.photopickerplus.util.intent.PhotoPickerPlusIntentWrapper.java
com.chute.android.photopickerplus.util.intent.PhotosIntentWrapper.java
com.example.productwidgetexample.BazaarFunctions.java
com.example.productwidgetexample.BazaarProduct.java
com.example.productwidgetexample.BazaarReview.java
com.example.productwidgetexample.BazaarUIThreadResponse.java
com.example.productwidgetexample.ImageDownloader.java
com.example.productwidgetexample.OnImageDownloadComplete.java
com.example.productwidgetexample.ProductWidgetProvider.java
com.example.productwidgetexample.ReviewAdapter.java
com.example.productwidgetexample.ReviewsActivity.java