Image.java revision 666ea1b28a76aeba74744148b15099254d918671
1/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.camera.gallery;
18
19import com.android.camera.Util;
20
21import android.content.ContentResolver;
22import android.content.ContentValues;
23import android.graphics.Bitmap;
24import android.graphics.BitmapFactory;
25import android.media.ExifInterface;
26import android.net.Uri;
27import android.provider.BaseColumns;
28import android.provider.MediaStore.Images;
29import android.provider.MediaStore.Images.ImageColumns;
30import android.util.Log;
31
32import java.io.IOException;
33
34/**
35 * The class for normal images in gallery.
36 */
37public class Image extends BaseImage implements IImage {
38    private static final String TAG = "BaseImage";
39
40    private ExifInterface mExif;
41
42    private int mRotation;
43
44    public Image(BaseImageList container, ContentResolver cr,
45            long id, int index, Uri uri, String dataPath, long miniThumbMagic,
46            String mimeType, long dateTaken, String title, String displayName,
47            int rotation) {
48        super(container, cr, id, index, uri, dataPath, miniThumbMagic,
49                mimeType, dateTaken, title, displayName);
50        mRotation = rotation;
51    }
52
53    @Override
54    public int getDegreesRotated() {
55        return mRotation;
56    }
57
58    protected void setDegreesRotated(int degrees) {
59        if (mRotation == degrees) return;
60        mRotation = degrees;
61        ContentValues values = new ContentValues();
62        values.put(ImageColumns.ORIENTATION, mRotation);
63        mContentResolver.update(mUri, values, null, null);
64
65        //TODO: Consider invalidate the cursor in container
66        // ((BaseImageList) getContainer()).invalidateCursor();
67    }
68
69    public boolean isReadonly() {
70        String mimeType = getMimeType();
71        return !"image/jpeg".equals(mimeType) && !"image/png".equals(mimeType);
72    }
73
74    public boolean isDrm() {
75        return false;
76    }
77
78    /**
79     * Replaces the tag if already there. Otherwise, adds to the exif tags.
80     * @param tag
81     * @param value
82     */
83    public void replaceExifTag(String tag, String value) {
84        if (mExif == null) {
85            loadExifData();
86        }
87        mExif.setAttribute(tag, value);
88    }
89
90    private void loadExifData() {
91        try {
92            mExif = new ExifInterface(mDataPath);
93        } catch (IOException ex) {
94            Log.e(TAG, "cannot read exif", ex);
95        }
96    }
97
98    private void saveExifData() throws IOException {
99        if (mExif != null) {
100            mExif.saveAttributes();
101        }
102    }
103
104    private void setExifRotation(int degrees) {
105        try {
106            degrees %= 360;
107            if (degrees < 0) degrees += 360;
108
109            int orientation = ExifInterface.ORIENTATION_NORMAL;
110            switch (degrees) {
111                case 0:
112                    orientation = ExifInterface.ORIENTATION_NORMAL;
113                    break;
114                case 90:
115                    orientation = ExifInterface.ORIENTATION_ROTATE_90;
116                    break;
117                case 180:
118                    orientation = ExifInterface.ORIENTATION_ROTATE_180;
119                    break;
120                case 270:
121                    orientation = ExifInterface.ORIENTATION_ROTATE_270;
122                    break;
123            }
124
125            replaceExifTag(ExifInterface.TAG_ORIENTATION,
126                    Integer.toString(orientation));
127            saveExifData();
128        } catch (Exception ex) {
129            Log.e(TAG, "unable to save exif data with new orientation "
130                    + fullSizeImageUri(), ex);
131        }
132    }
133
134    /**
135     * Save the rotated image by updating the Exif "Orientation" tag.
136     * @param degrees
137     */
138    public boolean rotateImageBy(int degrees) {
139        int newDegrees = (getDegreesRotated() + degrees) % 360;
140        setExifRotation(newDegrees);
141        setDegreesRotated(newDegrees);
142
143        return true;
144    }
145
146    private static final String[] THUMB_PROJECTION = new String[] {
147        BaseColumns._ID,
148    };
149
150    public Bitmap thumbBitmap(boolean rotateAsNeeded) {
151        Bitmap bitmap = null;
152        BitmapFactory.Options options = new BitmapFactory.Options();
153        options.inDither = false;
154        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
155        bitmap = Images.Thumbnails.getThumbnail(
156                    mContentResolver, mId, Images.Thumbnails.MINI_KIND, options);
157
158        if (bitmap != null && rotateAsNeeded) {
159            bitmap = Util.rotate(bitmap, getDegreesRotated());
160        }
161
162        return bitmap;
163    }
164}
165