Android Open Source - image-crop Image






From Project

Back to project page image-crop.

License

The source code is released under:

Apache License

If you think the Android project image-crop 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

/*
 * Copyright (C) 2009 The Android Open Source Project
 *//from ww  w. j a  v  a  2 s.c o m
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mobilesolutionworks.android.cropimage.camera.gallery;

import com.mobilesolutionworks.android.cropimage.camera.BitmapManager;
import com.mobilesolutionworks.android.cropimage.camera.Util;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.ExifInterface;
import android.net.Uri;
import android.provider.BaseColumns;
import android.provider.MediaStore.Images;
import android.provider.MediaStore.Images.ImageColumns;
import android.util.Log;

import java.io.IOException;

/**
 * The class for normal images in gallery.
 */
public class Image extends BaseImage implements IImage {
    private static final String TAG = "BaseImage";

    private ExifInterface mExif;

    private int mRotation;

    public Image(BaseImageList container, ContentResolver cr,
            long id, int index, Uri uri, String dataPath,
            String mimeType, long dateTaken, String title,
            int rotation) {
        super(container, cr, id, index, uri, dataPath,
                mimeType, dateTaken, title);
        mRotation = rotation;
    }

    @Override
    public int getDegreesRotated() {
        return mRotation;
    }

    protected void setDegreesRotated(int degrees) {
        if (mRotation == degrees) return;
        mRotation = degrees;
        ContentValues values = new ContentValues();
        values.put(ImageColumns.ORIENTATION, mRotation);
        mContentResolver.update(mUri, values, null, null);

        //TODO: Consider invalidate the cursor in container
        // ((BaseImageList) getContainer()).invalidateCursor();
    }

    public boolean isReadonly() {
        String mimeType = getMimeType();
        return !"image/jpeg".equals(mimeType) && !"image/png".equals(mimeType);
    }

    public boolean isDrm() {
        return false;
    }

    /**
     * Replaces the tag if already there. Otherwise, adds to the exif tags.
     * @param tag
     * @param value
     */
    public void replaceExifTag(String tag, String value) {
        if (mExif == null) {
            loadExifData();
        }
        mExif.setAttribute(tag, value);
    }

    private void loadExifData() {
        try {
            mExif = new ExifInterface(mDataPath);
        } catch (IOException ex) {
            Log.e(TAG, "cannot read exif", ex);
        }
    }

    private void saveExifData() throws IOException {
        if (mExif != null) {
            mExif.saveAttributes();
        }
    }

    private void setExifRotation(int degrees) {
        try {
            degrees %= 360;
            if (degrees < 0) degrees += 360;

            int orientation = ExifInterface.ORIENTATION_NORMAL;
            switch (degrees) {
                case 0:
                    orientation = ExifInterface.ORIENTATION_NORMAL;
                    break;
                case 90:
                    orientation = ExifInterface.ORIENTATION_ROTATE_90;
                    break;
                case 180:
                    orientation = ExifInterface.ORIENTATION_ROTATE_180;
                    break;
                case 270:
                    orientation = ExifInterface.ORIENTATION_ROTATE_270;
                    break;
            }

            replaceExifTag(ExifInterface.TAG_ORIENTATION,
                    Integer.toString(orientation));
            saveExifData();
        } catch (Exception ex) {
            Log.e(TAG, "unable to save exif data with new orientation "
                    + fullSizeImageUri(), ex);
        }
    }

    /**
     * Save the rotated image by updating the Exif "Orientation" tag.
     * @param degrees
     */
    public boolean rotateImageBy(int degrees) {
        int newDegrees = (getDegreesRotated() + degrees) % 360;
        setExifRotation(newDegrees);
        setDegreesRotated(newDegrees);

        return true;
    }

    private static final String[] THUMB_PROJECTION = new String[] {
        BaseColumns._ID,
    };

    public Bitmap thumbBitmap(boolean rotateAsNeeded) {
        Bitmap bitmap = null;
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inDither = false;
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        bitmap = BitmapManager.instance().getThumbnail(mContentResolver, mId,
                Images.Thumbnails.MINI_KIND, options, false);

        if (bitmap != null && rotateAsNeeded) {
            bitmap = Util.rotate(bitmap, getDegreesRotated());
        }

        return bitmap;
    }
}




Java Source Code List

com.mobilesolutionworks.android.cropimage.camera.BitmapManager.java
com.mobilesolutionworks.android.cropimage.camera.CropImageIntentBuilder.java
com.mobilesolutionworks.android.cropimage.camera.CropImageView.java
com.mobilesolutionworks.android.cropimage.camera.CropImage.java
com.mobilesolutionworks.android.cropimage.camera.Exif.java
com.mobilesolutionworks.android.cropimage.camera.HighlightView.java
com.mobilesolutionworks.android.cropimage.camera.ImageManager.java
com.mobilesolutionworks.android.cropimage.camera.ImageViewTouchBase.java
com.mobilesolutionworks.android.cropimage.camera.MonitoredActivity.java
com.mobilesolutionworks.android.cropimage.camera.NoSearchActivity.java
com.mobilesolutionworks.android.cropimage.camera.RotateBitmap.java
com.mobilesolutionworks.android.cropimage.camera.Util.java
com.mobilesolutionworks.android.cropimage.camera.gallery.BaseImageList.java
com.mobilesolutionworks.android.cropimage.camera.gallery.BaseImage.java
com.mobilesolutionworks.android.cropimage.camera.gallery.IImageList.java
com.mobilesolutionworks.android.cropimage.camera.gallery.IImage.java
com.mobilesolutionworks.android.cropimage.camera.gallery.ImageListUber.java
com.mobilesolutionworks.android.cropimage.camera.gallery.ImageList.java
com.mobilesolutionworks.android.cropimage.camera.gallery.Image.java
com.mobilesolutionworks.android.cropimage.camera.gallery.LruCache.java
com.mobilesolutionworks.android.cropimage.camera.gallery.SingleImageList.java
com.mobilesolutionworks.android.cropimage.camera.gallery.UriImage.java
com.mobilesolutionworks.android.cropimage.camera.gallery.VideoList.java
com.mobilesolutionworks.android.cropimage.camera.gallery.VideoObject.java