1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
|
/*
* Copyright (C) 2009 The Android Open Source Project
*
* 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.android.camera.gallery;
import com.android.camera.BitmapManager;
import com.android.camera.ExifInterface;
import com.android.camera.Util;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.ParcelFileDescriptor;
import android.provider.BaseColumns;
import android.provider.MediaStore.Images.Thumbnails;
import android.util.Log;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
/**
* The class for normal images in gallery.
*/
public class Image extends BaseImage implements IImage {
private static final String TAG = "BaseImage";
private int mRotation;
private ExifInterface mExif;
public Image(long id, long miniThumbMagic, ContentResolver cr,
BaseImageList container, int cursorRow, int rotation) {
super(id, miniThumbMagic, cr, container, cursorRow);
mRotation = rotation;
}
public String getDataPath() {
String path = null;
Cursor c = getCursor();
synchronized (c) {
if (c.moveToPosition(getRow())) {
int column = ((ImageList) getContainer()).indexData();
if (column >= 0)
path = c.getString(column);
}
}
return path;
}
@Override
protected int getDegreesRotated() {
return mRotation;
}
protected void setDegreesRotated(int degrees) {
Cursor c = getCursor();
mRotation = degrees;
synchronized (c) {
if (c.moveToPosition(getRow())) {
int column = ((ImageList) getContainer()).indexOrientation();
if (column >= 0) {
c.updateInt(column, degrees);
c.commitUpdates();
}
}
}
}
@Override
protected Bitmap.CompressFormat compressionType() {
String mimeType = getMimeType();
if ("image/png".equals(mimeType)) {
return Bitmap.CompressFormat.PNG;
} else if ("image/gif".equals(mimeType)) {
return Bitmap.CompressFormat.PNG;
}
return Bitmap.CompressFormat.JPEG;
}
/**
* Does not replace the tag if already there. Otherwise, adds to the exif
* tags.
*
* @param tag
* @param value
*/
public void addExifTag(String tag, String value) {
if (mExifData == null) {
loadExifData();
}
// If the key is already there, ignore it.
if (!mExifData.containsKey(tag)) {
mExifData.put(tag, value);
}
}
/**
* Return the value of the Exif tag as an int. Returns 0 on any type of
* error.
*
* @param tag
*/
public int getExifTagInt(String tag, int defaultValue) {
String tagValue = getExifTag(tag);
try {
if (tagValue != null) {
return Integer.parseInt(tagValue);
}
} catch (NumberFormatException ex) {
// Simply return defaultValue if exception is thrown.
Log.v(TAG, ex.toString());
}
return defaultValue;
}
/**
* Return the value of the Exif tag as a String. It's caller's
* responsibility to check nullity.
*/
public String getExifTag(String tag) {
if (mExifData == null) {
loadExifData();
}
return mExifData.get(tag);
}
public boolean isReadonly() {
String mimeType = getMimeType();
return !"image/jpeg".equals(mimeType) && !"image/png".equals(mimeType);
}
public boolean isDrm() {
return false;
}
/**
* Remove tag if already there. Otherwise, does nothing.
* @param tag
*/
public void removeExifTag(String tag) {
if (mExifData == null) {
loadExifData();
}
mExifData.remove(tag);
}
/**
* Replaces the tag if already there. Otherwise, adds to the exif tags.
* @param tag
* @param value
*/
public void replaceExifTag(String tag, String value) {
if (mExifData == null) {
loadExifData();
}
mExifData.put(tag, value);
}
private class SaveImageContentsCancelable extends BaseCancelable<Void> {
private final Bitmap mImage;
private final byte [] mJpegData;
private final int mOrientation;
private final String mFilePath;
SaveImageContentsCancelable(Bitmap image, byte[] jpegData,
int orientation, String filePath) {
mImage = image;
mJpegData = jpegData;
mOrientation = orientation;
mFilePath = filePath;
}
@Override
public Void execute() throws InterruptedException, ExecutionException {
Bitmap thumbnail = null;
Uri uri = mContainer.contentUri(mId);
runSubTask(compressImageToFile(mImage, mJpegData, uri));
synchronized (this) {
String filePath = mFilePath;
// TODO: If thumbData is present and usable, we should call
// the version of storeThumbnail which takes a byte array,
// rather than re-encoding a new JPEG of the same
// dimensions.
byte[] thumbData = null;
synchronized (ExifInterface.class) {
thumbData =
(new ExifInterface(filePath)).getThumbnail();
}
if (thumbData != null) {
thumbnail = BitmapFactory.decodeByteArray(
thumbData, 0, thumbData.length);
}
if (thumbnail == null && mImage != null) {
thumbnail = mImage;
}
if (thumbnail == null && mJpegData != null) {
thumbnail = BitmapFactory.decodeByteArray(
mJpegData, 0, mJpegData.length);
}
}
mContainer.storeThumbnail(
thumbnail, Image.this.fullSizeImageId());
if (isCanceling()) return null;
try {
thumbnail = Util.rotate(thumbnail, mOrientation);
saveMiniThumb(thumbnail);
} catch (IOException e) {
// Ignore if unable to save thumb.
Log.e(TAG, "unable to rotate / save thumb", e);
}
return null;
}
}
public Cancelable<Void> saveImageContents(Bitmap image, byte [] jpegData,
int orientation, boolean newFile, String filePath) {
return new SaveImageContentsCancelable(
image, jpegData, orientation, filePath);
}
private void loadExifData() {
Cursor c = getCursor();
String filePath;
synchronized (c) {
filePath = c.getString(mContainer.indexData());
}
ExifInterface mExif = new ExifInterface(filePath);
if (mExifData == null) {
mExifData = mExif.getAttributes();
}
}
private void saveExifData() {
if (mExif != null && mExifData != null) {
mExif.saveAttributes(mExifData);
}
}
private void setExifRotation(int degrees) {
try {
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));
replaceExifTag("UserComment",
"saveRotatedImage comment orientation: " + orientation);
saveExifData();
} catch (RuntimeException ex) {
Log.e(TAG, "unable to save exif data with new orientation "
+ fullSizeImageUri());
}
}
/**
* Save the rotated image by updating the Exif "Orientation" tag.
* @param degrees
*/
public boolean rotateImageBy(int degrees) {
int newDegrees = getDegreesRotated() + degrees;
setExifRotation(newDegrees);
setDegreesRotated(newDegrees);
// setting this to zero will force the call to checkCursor to generate
// fresh thumbs
mMiniThumbMagic = 0;
try {
mContainer.checkThumbnail(this, this.getRow(), null);
} catch (IOException e) {
// Ignore inability to store mini thumbnail.
}
return true;
}
private static final String[] THUMB_PROJECTION = new String[] {
BaseColumns._ID,
};
public Bitmap thumbBitmap() {
Bitmap bitmap = null;
if (mContainer.mThumbUri != null) {
Cursor c = mContentResolver.query(
mContainer.mThumbUri, THUMB_PROJECTION,
Thumbnails.IMAGE_ID + "=?",
new String[] { String.valueOf(fullSizeImageId()) },
null);
try {
if (c.moveToFirst()) {
bitmap = decodeCurrentImage(c.getLong(0));
}
} catch (RuntimeException ex) {
// sdcard removed?
return null;
} finally {
c.close();
}
}
if (bitmap == null) {
bitmap = fullSizeBitmap(THUMBNAIL_TARGET_SIZE, false);
// No thumbnail found... storing the new one.
bitmap = mContainer.storeThumbnail(bitmap, fullSizeImageId());
}
if (bitmap != null) {
bitmap = Util.rotate(bitmap, getDegreesRotated());
}
return bitmap;
}
private Bitmap decodeCurrentImage(long id) {
Uri thumbUri = ContentUris.withAppendedId(
mContainer.mThumbUri, id);
ParcelFileDescriptor pfdInput;
Bitmap bitmap = null;
try {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inDither = false;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
pfdInput = mContentResolver.openFileDescriptor(thumbUri, "r");
bitmap = BitmapManager.instance().decodeFileDescriptor(
pfdInput.getFileDescriptor(), options);
pfdInput.close();
} catch (FileNotFoundException ex) {
Log.e(TAG, "couldn't open thumbnail " + thumbUri + "; " + ex);
} catch (IOException ex) {
Log.e(TAG, "couldn't open thumbnail " + thumbUri + "; " + ex);
} catch (NullPointerException ex) {
// we seem to get this if the file doesn't exist anymore
Log.e(TAG, "couldn't open thumbnail " + thumbUri + "; " + ex);
} catch (OutOfMemoryError ex) {
Log.e(TAG, "failed to allocate memory for thumbnail "
+ thumbUri + "; " + ex);
}
return bitmap;
}
}
|