|
|
@ -7,7 +7,8 @@ import android.graphics.BitmapFactory; |
|
|
|
import android.graphics.Matrix; |
|
|
|
import android.graphics.Matrix; |
|
|
|
import android.hardware.Camera; |
|
|
|
import android.hardware.Camera; |
|
|
|
import android.os.Handler; |
|
|
|
import android.os.Handler; |
|
|
|
import android.support.annotation.UiThread; |
|
|
|
import android.support.annotation.NonNull; |
|
|
|
|
|
|
|
import android.support.annotation.Nullable; |
|
|
|
import android.support.annotation.WorkerThread; |
|
|
|
import android.support.annotation.WorkerThread; |
|
|
|
import android.support.media.ExifInterface; |
|
|
|
import android.support.media.ExifInterface; |
|
|
|
|
|
|
|
|
|
|
@ -116,12 +117,17 @@ public class CameraUtils { |
|
|
|
* @param callback a callback to be notified |
|
|
|
* @param callback a callback to be notified |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@SuppressWarnings("WeakerAccess") |
|
|
|
@SuppressWarnings("WeakerAccess") |
|
|
|
public static void decodeBitmap(final byte[] source, final int maxWidth, final int maxHeight, final BitmapFactory.Options options, final BitmapCallback callback) { |
|
|
|
public static void decodeBitmap(final byte[] source, final int maxWidth, final int maxHeight, @NonNull final BitmapFactory.Options options, final BitmapCallback callback) { |
|
|
|
|
|
|
|
decodeBitmap(source, maxWidth, maxHeight, options, -1, callback); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("WeakerAccess") |
|
|
|
|
|
|
|
static void decodeBitmap(final byte[] source, final int maxWidth, final int maxHeight, @NonNull final BitmapFactory.Options options, final int rotation, final BitmapCallback callback) { |
|
|
|
final Handler ui = new Handler(); |
|
|
|
final Handler ui = new Handler(); |
|
|
|
WorkerHandler.run(new Runnable() { |
|
|
|
WorkerHandler.run(new Runnable() { |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void run() { |
|
|
|
public void run() { |
|
|
|
final Bitmap bitmap = decodeBitmap(source, maxWidth, maxHeight, options); |
|
|
|
final Bitmap bitmap = decodeBitmap(source, maxWidth, maxHeight, options, rotation); |
|
|
|
ui.post(new Runnable() { |
|
|
|
ui.post(new Runnable() { |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void run() { |
|
|
|
public void run() { |
|
|
@ -159,67 +165,85 @@ public class CameraUtils { |
|
|
|
* @param maxHeight the max allowed height |
|
|
|
* @param maxHeight the max allowed height |
|
|
|
* @param options the options to be passed to decodeByteArray |
|
|
|
* @param options the options to be passed to decodeByteArray |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
// TODO ignores flipping
|
|
|
|
|
|
|
|
@SuppressWarnings({"SuspiciousNameCombination", "WeakerAccess"}) |
|
|
|
@SuppressWarnings({"SuspiciousNameCombination", "WeakerAccess"}) |
|
|
|
|
|
|
|
@Nullable |
|
|
|
@WorkerThread |
|
|
|
@WorkerThread |
|
|
|
public static Bitmap decodeBitmap(byte[] source, int maxWidth, int maxHeight, BitmapFactory.Options options) { |
|
|
|
public static Bitmap decodeBitmap(byte[] source, int maxWidth, int maxHeight, @NonNull BitmapFactory.Options options) { |
|
|
|
|
|
|
|
return decodeBitmap(source, maxWidth, maxHeight, options, -1); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Null: got OOM
|
|
|
|
|
|
|
|
// TODO ignores flipping. but it should be super rare.
|
|
|
|
|
|
|
|
@Nullable |
|
|
|
|
|
|
|
static Bitmap decodeBitmap(byte[] source, int maxWidth, int maxHeight, @NonNull BitmapFactory.Options options, int rotation) { |
|
|
|
if (maxWidth <= 0) maxWidth = Integer.MAX_VALUE; |
|
|
|
if (maxWidth <= 0) maxWidth = Integer.MAX_VALUE; |
|
|
|
if (maxHeight <= 0) maxHeight = Integer.MAX_VALUE; |
|
|
|
if (maxHeight <= 0) maxHeight = Integer.MAX_VALUE; |
|
|
|
int orientation; |
|
|
|
int orientation; |
|
|
|
boolean flip; |
|
|
|
boolean flip; |
|
|
|
InputStream stream = null; |
|
|
|
if (rotation == -1) { |
|
|
|
try { |
|
|
|
InputStream stream = null; |
|
|
|
// http://sylvana.net/jpegcrop/exif_orientation.html
|
|
|
|
try { |
|
|
|
stream = new ByteArrayInputStream(source); |
|
|
|
// http://sylvana.net/jpegcrop/exif_orientation.html
|
|
|
|
ExifInterface exif = new ExifInterface(stream); |
|
|
|
stream = new ByteArrayInputStream(source); |
|
|
|
int exifOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); |
|
|
|
ExifInterface exif = new ExifInterface(stream); |
|
|
|
orientation = decodeExifOrientation(exifOrientation); |
|
|
|
int exifOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); |
|
|
|
flip = exifOrientation == ExifInterface.ORIENTATION_FLIP_HORIZONTAL || |
|
|
|
orientation = readExifOrientation(exifOrientation); |
|
|
|
exifOrientation == ExifInterface.ORIENTATION_FLIP_VERTICAL || |
|
|
|
flip = exifOrientation == ExifInterface.ORIENTATION_FLIP_HORIZONTAL || |
|
|
|
exifOrientation == ExifInterface.ORIENTATION_TRANSPOSE || |
|
|
|
exifOrientation == ExifInterface.ORIENTATION_FLIP_VERTICAL || |
|
|
|
exifOrientation == ExifInterface.ORIENTATION_TRANSVERSE; |
|
|
|
exifOrientation == ExifInterface.ORIENTATION_TRANSPOSE || |
|
|
|
|
|
|
|
exifOrientation == ExifInterface.ORIENTATION_TRANSVERSE; |
|
|
|
|
|
|
|
|
|
|
|
} catch (IOException e) { |
|
|
|
} catch (IOException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
e.printStackTrace(); |
|
|
|
orientation = 0; |
|
|
|
orientation = 0; |
|
|
|
flip = false; |
|
|
|
flip = false; |
|
|
|
} finally { |
|
|
|
} finally { |
|
|
|
if (stream != null) { |
|
|
|
if (stream != null) { |
|
|
|
try { stream.close(); } catch (Exception ignored) {} |
|
|
|
try { |
|
|
|
|
|
|
|
stream.close(); |
|
|
|
|
|
|
|
} catch (Exception ignored) { } |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
orientation = rotation; |
|
|
|
|
|
|
|
flip = false; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Bitmap bitmap; |
|
|
|
Bitmap bitmap; |
|
|
|
if (maxWidth < Integer.MAX_VALUE || maxHeight < Integer.MAX_VALUE) { |
|
|
|
try { |
|
|
|
options.inJustDecodeBounds = true; |
|
|
|
if (maxWidth < Integer.MAX_VALUE || maxHeight < Integer.MAX_VALUE) { |
|
|
|
BitmapFactory.decodeByteArray(source, 0, source.length, options); |
|
|
|
options.inJustDecodeBounds = true; |
|
|
|
|
|
|
|
BitmapFactory.decodeByteArray(source, 0, source.length, options); |
|
|
|
|
|
|
|
|
|
|
|
int outHeight = options.outHeight; |
|
|
|
int outHeight = options.outHeight; |
|
|
|
int outWidth = options.outWidth; |
|
|
|
int outWidth = options.outWidth; |
|
|
|
if (orientation % 180 != 0) { |
|
|
|
if (orientation % 180 != 0) { |
|
|
|
outHeight = options.outWidth; |
|
|
|
//noinspection SuspiciousNameCombination
|
|
|
|
outWidth = options.outHeight; |
|
|
|
outHeight = options.outWidth; |
|
|
|
} |
|
|
|
outWidth = options.outHeight; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
options.inSampleSize = computeSampleSize(outWidth, outHeight, maxWidth, maxHeight); |
|
|
|
options.inSampleSize = computeSampleSize(outWidth, outHeight, maxWidth, maxHeight); |
|
|
|
options.inJustDecodeBounds = false; |
|
|
|
options.inJustDecodeBounds = false; |
|
|
|
bitmap = BitmapFactory.decodeByteArray(source, 0, source.length, options); |
|
|
|
bitmap = BitmapFactory.decodeByteArray(source, 0, source.length, options); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
bitmap = BitmapFactory.decodeByteArray(source, 0, source.length); |
|
|
|
bitmap = BitmapFactory.decodeByteArray(source, 0, source.length); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (orientation != 0 || flip) { |
|
|
|
if (orientation != 0 || flip) { |
|
|
|
Matrix matrix = new Matrix(); |
|
|
|
Matrix matrix = new Matrix(); |
|
|
|
matrix.setRotate(orientation); |
|
|
|
matrix.setRotate(orientation); |
|
|
|
// matrix.postScale(1, -1) Flip... needs testing.
|
|
|
|
Bitmap temp = bitmap; |
|
|
|
Bitmap temp = bitmap; |
|
|
|
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); |
|
|
|
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); |
|
|
|
temp.recycle(); |
|
|
|
temp.recycle(); |
|
|
|
} |
|
|
|
|
|
|
|
} catch (OutOfMemoryError e) { |
|
|
|
|
|
|
|
bitmap = null; |
|
|
|
} |
|
|
|
} |
|
|
|
return bitmap; |
|
|
|
return bitmap; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static int decodeExifOrientation(int exifOrientation) { |
|
|
|
static int readExifOrientation(int exifOrientation) { |
|
|
|
int orientation; |
|
|
|
int orientation; |
|
|
|
switch (exifOrientation) { |
|
|
|
switch (exifOrientation) { |
|
|
|
case ExifInterface.ORIENTATION_NORMAL: |
|
|
|
case ExifInterface.ORIENTATION_NORMAL: |
|
|
|