Skip to content

Commit

Permalink
update.
Browse files Browse the repository at this point in the history
  • Loading branch information
郑晓勇 committed Apr 27, 2017
1 parent 92c1924 commit 9fdc8b1
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 56 deletions.
107 changes: 51 additions & 56 deletions tiny/src/main/java/com/zxy/tiny/core/BitmapCompressor.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import com.zxy.tiny.Tiny;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

Expand All @@ -33,21 +32,30 @@ public static Bitmap compress(Bitmap bitmap, Tiny.BitmapCompressOptions options,

Pair<Integer, Integer> screenPair = CompressKit.getDeviceScreenSizeInPixels();

boolean hasCustomSize = false;

if (compressWidth > 0 && compressHeight > 0)
hasCustomSize = true;

int baseline;
if (isViewMode) {
if (screenPair.second >= CompressKit.DEFAULT_MAX_COMPRESS_SIZE ||
screenPair.first >= CompressKit.DEFAULT_MAX_COMPRESS_SIZE) {
result = matrixCompress(bitmap, CompressKit.DEFAULT_MAX_COMPRESS_SIZE, false);
baseline = hasCustomSize ? Math.min(CompressKit.DEFAULT_MAX_COMPRESS_SIZE, Math.max(compressWidth, compressHeight)) : CompressKit.DEFAULT_MAX_COMPRESS_SIZE;
result = matrixCompress(bitmap, baseline, false);
} else {
result = matrixCompress(bitmap, screenPair.second, false);
baseline = hasCustomSize ? Math.min(CompressKit.DEFAULT_MAX_COMPRESS_SIZE, Math.max(compressWidth, compressHeight)) : screenPair.second;
result = matrixCompress(bitmap, baseline, false);
}
} else {
result = matrixCompress(bitmap, CompressKit.DEFAULT_MAX_COMPRESS_SIZE, false);
baseline = hasCustomSize ? Math.min(CompressKit.DEFAULT_MAX_COMPRESS_SIZE, Math.max(compressWidth, compressHeight)) : CompressKit.DEFAULT_MAX_COMPRESS_SIZE;
result = matrixCompress(bitmap, baseline, false);
}

if (result == null)
return null;

if (compressWidth > 0 && compressHeight > 0) {
if (hasCustomSize) {
result = customCompress(result, compressWidth, compressHeight, false);
}

Expand Down Expand Up @@ -75,33 +83,36 @@ public static Bitmap compress(byte[] bytes, Tiny.BitmapCompressOptions options,

Pair<Integer, Integer> screenPair = CompressKit.getDeviceScreenSizeInPixels();

boolean hasCustomSize = false;

if (compressWidth > 0 && compressHeight > 0)
hasCustomSize = true;

int baseline;
if (isViewMode) {
if (screenPair.second >= CompressKit.DEFAULT_MAX_COMPRESS_SIZE ||
screenPair.first >= CompressKit.DEFAULT_MAX_COMPRESS_SIZE) {
int sampleSize = computeSampleSize(bitmapWidth, bitmapHeight, CompressKit.DEFAULT_MAX_COMPRESS_SIZE);
result = matrixCompress(sampleCompress(bytes, sampleSize, options), CompressKit.DEFAULT_MAX_COMPRESS_SIZE, true);
baseline = hasCustomSize ? Math.min(CompressKit.DEFAULT_MAX_COMPRESS_SIZE, Math.max(compressWidth, compressHeight)) : CompressKit.DEFAULT_MAX_COMPRESS_SIZE;
int sampleSize = computeSampleSize(bitmapWidth, bitmapHeight, baseline);
result = matrixCompress(sampleCompress(bytes, sampleSize, options), baseline, true);
} else {
int sampleSize = computeSampleSize(bitmapWidth, bitmapHeight, screenPair.second);
result = matrixCompress(sampleCompress(bytes, sampleSize, options), screenPair.second, true);
baseline = hasCustomSize ? Math.min(CompressKit.DEFAULT_MAX_COMPRESS_SIZE, Math.max(compressWidth, compressHeight)) : screenPair.second;
int sampleSize = computeSampleSize(bitmapWidth, bitmapHeight, baseline);
result = matrixCompress(sampleCompress(bytes, sampleSize, options), baseline, true);
}
} else {
int sampleSize = computeSampleSize(bitmapWidth, bitmapHeight, CompressKit.DEFAULT_MAX_COMPRESS_SIZE);
result = matrixCompress(sampleCompress(bytes, sampleSize, options), CompressKit.DEFAULT_MAX_COMPRESS_SIZE, true);
baseline = hasCustomSize ? Math.min(CompressKit.DEFAULT_MAX_COMPRESS_SIZE, Math.max(compressWidth, compressHeight)) : CompressKit.DEFAULT_MAX_COMPRESS_SIZE;
int sampleSize = computeSampleSize(bitmapWidth, bitmapHeight, baseline);
result = matrixCompress(sampleCompress(bytes, sampleSize, options), baseline, true);
}

if (result == null)
return null;

if (compressWidth > 0 && compressHeight > 0) {
if (hasCustomSize) {
result = customCompress(result, compressWidth, compressHeight, true);
}

//processing bitmap degree.
if (ExifCompat.isJpeg(bytes)) {
int orientation = ExifCompat.getOrientation(bytes);
result = BitmapKit.rotateBitmap(result, orientation);
}

return result;
}

Expand All @@ -123,56 +134,36 @@ public static Bitmap compress(int resId, Tiny.BitmapCompressOptions options, boo

Pair<Integer, Integer> screenPair = CompressKit.getDeviceScreenSizeInPixels();

boolean hasCustomSize = false;

if (compressWidth > 0 && compressHeight > 0)
hasCustomSize = true;

int baseline;
if (isViewMode) {
if (screenPair.second >= CompressKit.DEFAULT_MAX_COMPRESS_SIZE ||
screenPair.first >= CompressKit.DEFAULT_MAX_COMPRESS_SIZE) {
int sampleSize = computeSampleSize(bitmapWidth, bitmapHeight, CompressKit.DEFAULT_MAX_COMPRESS_SIZE);
result = matrixCompress(sampleCompress(resId, sampleSize, options), CompressKit.DEFAULT_MAX_COMPRESS_SIZE, true);
baseline = hasCustomSize ? Math.min(CompressKit.DEFAULT_MAX_COMPRESS_SIZE, Math.max(compressWidth, compressHeight)) : CompressKit.DEFAULT_MAX_COMPRESS_SIZE;
int sampleSize = computeSampleSize(bitmapWidth, bitmapHeight, baseline);
result = matrixCompress(sampleCompress(resId, sampleSize, options), baseline, true);
} else {
int sampleSize = computeSampleSize(bitmapWidth, bitmapHeight, screenPair.second);
result = matrixCompress(sampleCompress(resId, sampleSize, options), screenPair.second, true);
baseline = hasCustomSize ? Math.min(CompressKit.DEFAULT_MAX_COMPRESS_SIZE, Math.max(compressWidth, compressHeight)) : screenPair.second;
int sampleSize = computeSampleSize(bitmapWidth, bitmapHeight, baseline);
result = matrixCompress(sampleCompress(resId, sampleSize, options), baseline, true);
}
} else {
int sampleSize = computeSampleSize(bitmapWidth, bitmapHeight, CompressKit.DEFAULT_MAX_COMPRESS_SIZE);
result = matrixCompress(sampleCompress(resId, sampleSize, options), CompressKit.DEFAULT_MAX_COMPRESS_SIZE, true);
baseline = hasCustomSize ? Math.min(CompressKit.DEFAULT_MAX_COMPRESS_SIZE, Math.max(compressWidth, compressHeight)) : CompressKit.DEFAULT_MAX_COMPRESS_SIZE;
int sampleSize = computeSampleSize(bitmapWidth, bitmapHeight, baseline);
result = matrixCompress(sampleCompress(resId, sampleSize, options), baseline, true);
}

if (result == null)
return null;

if (compressWidth > 0 && compressHeight > 0) {
if (hasCustomSize) {
result = customCompress(result, compressWidth, compressHeight, true);
}

//processing bitmap degree.
InputStream is = null;
Resources resources = Tiny.getInstance().getApplication().getResources();
try {
is = resources.openRawResource(resId, new TypedValue());
ByteArrayOutputStream os = new ByteArrayOutputStream();
byte[] buffer = new byte[4096];
int len = -1;
while ((len = is.read(buffer)) != -1) {
os.write(buffer, 0, len);
}
os.close();

if (ExifCompat.isJpeg(os.toByteArray())) {
int orientation = ExifCompat.getOrientation(os.toByteArray());
result = BitmapKit.rotateBitmap(result, orientation);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
//ignore...
}
}
}

return result;
}

Expand Down Expand Up @@ -234,7 +225,9 @@ private static Bitmap sampleCompress(byte[] bytes, int sampleSize, Tiny.BitmapCo
BitmapFactory.Options decodeOptions = CompressKit.getDefaultDecodeOptions();
decodeOptions.inPreferredConfig = options.config;
decodeOptions.inSampleSize = sampleSize;
return BitmapFactory.decodeByteArray(bytes, 0, bytes.length, decodeOptions);
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length, decodeOptions);
bitmap = Degrees.handle(bitmap, bytes);
return bitmap;
}

public static Bitmap sampleCompress(int resId, int sampleSize, Tiny.BitmapCompressOptions options) {
Expand All @@ -245,7 +238,9 @@ public static Bitmap sampleCompress(int resId, int sampleSize, Tiny.BitmapCompre
BitmapFactory.Options decodeOptions = CompressKit.getDefaultDecodeOptions();
decodeOptions.inPreferredConfig = options.config;
decodeOptions.inSampleSize = sampleSize;
return BitmapFactory.decodeStream(is, null, decodeOptions);
Bitmap bitmap = BitmapFactory.decodeStream(is, null, decodeOptions);
bitmap = Degrees.handle(bitmap, resId);
return bitmap;
} catch (Exception e) {
e.printStackTrace();
} finally {
Expand Down
57 changes: 57 additions & 0 deletions tiny/src/main/java/com/zxy/tiny/core/Degrees.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.zxy.tiny.core;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.util.TypedValue;

import com.zxy.tiny.Tiny;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
* Created by zhengxiaoyong on 2017/4/27.
*/
public class Degrees {

public static Bitmap handle(Bitmap bitmap, byte[] data) {
if (ExifCompat.isJpeg(data)) {
int orientation = ExifCompat.getOrientation(data);
bitmap = BitmapKit.rotateBitmap(bitmap, orientation);
}
return bitmap;
}

public static Bitmap handle(Bitmap bitmap, int resId) {
InputStream is = null;
Resources resources = Tiny.getInstance().getApplication().getResources();
try {
is = resources.openRawResource(resId, new TypedValue());
ByteArrayOutputStream os = new ByteArrayOutputStream();
byte[] buffer = new byte[4096];
int len = -1;
while ((len = is.read(buffer)) != -1) {
os.write(buffer, 0, len);
}
os.close();

if (ExifCompat.isJpeg(os.toByteArray())) {
int orientation = ExifCompat.getOrientation(os.toByteArray());
bitmap = BitmapKit.rotateBitmap(bitmap, orientation);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
//ignore...
}
}
}
return bitmap;
}

}

0 comments on commit 9fdc8b1

Please sign in to comment.