diff options
author | Paul Westbrook <pwestbro@google.com> | 2012-09-25 15:31:26 -0700 |
---|---|---|
committer | Paul Westbrook <pwestbro@google.com> | 2012-09-25 15:31:26 -0700 |
commit | 508667e1bc23d18190c979799611a1927010cd01 (patch) | |
tree | 2c4d7c24346c118fa6c0d99971ec226ca735ab40 /photoviewer | |
parent | ef8d37e8cef5ba8f349baef79abf9510d86b4dd1 (diff) | |
parent | b3f0f056afba01a3d63ffc3bc27981a045c57672 (diff) | |
download | android_frameworks_ex-508667e1bc23d18190c979799611a1927010cd01.tar.gz android_frameworks_ex-508667e1bc23d18190c979799611a1927010cd01.tar.bz2 android_frameworks_ex-508667e1bc23d18190c979799611a1927010cd01.zip |
resolved conflicts for merge of b3f0f056 to master
Change-Id: I5def1804466ff971ed5bc5afd2a2d68e135d20a2
Diffstat (limited to 'photoviewer')
-rw-r--r-- | photoviewer/src/com/android/ex/photo/util/Exif.java | 137 | ||||
-rw-r--r-- | photoviewer/src/com/android/ex/photo/util/ImageUtils.java | 54 |
2 files changed, 188 insertions, 3 deletions
diff --git a/photoviewer/src/com/android/ex/photo/util/Exif.java b/photoviewer/src/com/android/ex/photo/util/Exif.java new file mode 100644 index 0000000..743b896 --- /dev/null +++ b/photoviewer/src/com/android/ex/photo/util/Exif.java @@ -0,0 +1,137 @@ +/* + * Copyright (C) 2010 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.ex.photo.util; + +import android.util.Log; + +public class Exif { + private static final String TAG = "CameraExif"; + + // Returns the degrees in clockwise. Values are 0, 90, 180, or 270. + public static int getOrientation(byte[] jpeg) { + if (jpeg == null) { + return 0; + } + + int offset = 0; + int length = 0; + + // ISO/IEC 10918-1:1993(E) + while (offset + 3 < jpeg.length && (jpeg[offset++] & 0xFF) == 0xFF) { + int marker = jpeg[offset] & 0xFF; + + // Check if the marker is a padding. + if (marker == 0xFF) { + continue; + } + offset++; + + // Check if the marker is SOI or TEM. + if (marker == 0xD8 || marker == 0x01) { + continue; + } + // Check if the marker is EOI or SOS. + if (marker == 0xD9 || marker == 0xDA) { + break; + } + + // Get the length and check if it is reasonable. + length = pack(jpeg, offset, 2, false); + if (length < 2 || offset + length > jpeg.length) { + Log.e(TAG, "Invalid length"); + return 0; + } + + // Break if the marker is EXIF in APP1. + if (marker == 0xE1 && length >= 8 && + pack(jpeg, offset + 2, 4, false) == 0x45786966 && + pack(jpeg, offset + 6, 2, false) == 0) { + offset += 8; + length -= 8; + break; + } + + // Skip other markers. + offset += length; + length = 0; + } + + // JEITA CP-3451 Exif Version 2.2 + if (length > 8) { + // Identify the byte order. + int tag = pack(jpeg, offset, 4, false); + if (tag != 0x49492A00 && tag != 0x4D4D002A) { + Log.e(TAG, "Invalid byte order"); + return 0; + } + boolean littleEndian = (tag == 0x49492A00); + + // Get the offset and check if it is reasonable. + int count = pack(jpeg, offset + 4, 4, littleEndian) + 2; + if (count < 10 || count > length) { + Log.e(TAG, "Invalid offset"); + return 0; + } + offset += count; + length -= count; + + // Get the count and go through all the elements. + count = pack(jpeg, offset - 2, 2, littleEndian); + while (count-- > 0 && length >= 12) { + // Get the tag and check if it is orientation. + tag = pack(jpeg, offset, 2, littleEndian); + if (tag == 0x0112) { + // We do not really care about type and count, do we? + int orientation = pack(jpeg, offset + 8, 2, littleEndian); + switch (orientation) { + case 1: + return 0; + case 3: + return 180; + case 6: + return 90; + case 8: + return 270; + } + Log.i(TAG, "Unsupported orientation"); + return 0; + } + offset += 12; + length -= 12; + } + } + + Log.i(TAG, "Orientation not found"); + return 0; + } + + private static int pack(byte[] bytes, int offset, int length, + boolean littleEndian) { + int step = 1; + if (littleEndian) { + offset += length - 1; + step = -1; + } + + int value = 0; + while (length-- > 0) { + value = (value << 8) | (bytes[offset] & 0xFF); + offset += step; + } + return value; + } +} diff --git a/photoviewer/src/com/android/ex/photo/util/ImageUtils.java b/photoviewer/src/com/android/ex/photo/util/ImageUtils.java index a9790eb..a932c8b 100644 --- a/photoviewer/src/com/android/ex/photo/util/ImageUtils.java +++ b/photoviewer/src/com/android/ex/photo/util/ImageUtils.java @@ -20,20 +20,26 @@ package com.android.ex.photo.util; import android.content.ContentResolver; import android.graphics.Bitmap; import android.graphics.BitmapFactory; +import android.graphics.Matrix; import android.graphics.Point; import android.graphics.Rect; import android.net.Uri; import android.os.Build; +import android.util.DisplayMetrics; import android.util.Log; import com.android.ex.photo.PhotoViewActivity; +import com.android.ex.photo.util.Exif; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; + /** * Image utilities */ @@ -93,7 +99,6 @@ public class ImageUtils { try { final BitmapFactory.Options opts = new BitmapFactory.Options(); final Point bounds = getImageBounds(resolver, uri); - inputStream = openInputStream(resolver, uri); opts.inSampleSize = Math.max(bounds.x / maxSize, bounds.y / maxSize); @@ -139,11 +144,55 @@ public class ImageUtils { * size be returned (in opts.outWidth and opts.outHeight) */ public static Bitmap decodeStream(InputStream is, Rect outPadding, BitmapFactory.Options opts) { + ByteArrayOutputStream out = null; + InputStream byteStream = null; try { - return BitmapFactory.decodeStream(is, outPadding, opts); + out = new ByteArrayOutputStream(); + final byte[] buffer = new byte[4096]; + int n = is.read(buffer); + while (n >= 0) { + out.write(buffer, 0, n); + n = is.read(buffer); + } + + final byte[] bitmapBytes = out.toByteArray(); + + // Determine the orientation for this image + final int orientation = Exif.getOrientation(bitmapBytes); + + // Create an InputStream from this byte array + byteStream = new ByteArrayInputStream(bitmapBytes); + + final Bitmap originalBitmap = BitmapFactory.decodeStream(byteStream, outPadding, opts); + + if (originalBitmap != null && orientation != 0) { + final Matrix matrix = new Matrix(); + matrix.postRotate(orientation); + return Bitmap.createBitmap(originalBitmap, 0, 0, originalBitmap.getWidth(), + originalBitmap.getHeight(), matrix, true); + } + return originalBitmap; } catch (OutOfMemoryError oome) { Log.e(TAG, "ImageUtils#decodeStream(InputStream, Rect, Options) threw an OOME", oome); return null; + } catch (IOException ioe) { + Log.e(TAG, "ImageUtils#decodeStream(InputStream, Rect, Options) threw an IOE", ioe); + return null; + } finally { + if (out != null) { + try { + out.close(); + } catch (IOException e) { + // Do nothing + } + } + if (byteStream != null) { + try { + byteStream.close(); + } catch (IOException e) { + // Do nothing + } + } } } @@ -194,4 +243,3 @@ public class ImageUtils { return resolver.openInputStream(uri); } } - |