Skip to content

Commit

Permalink
fix issue#29
Browse files Browse the repository at this point in the history
  • Loading branch information
郑晓勇 committed Jul 29, 2017
1 parent 91d4368 commit c71eb06
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 20 deletions.
2 changes: 1 addition & 1 deletion tiny/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ dependencies {

ext {
upload_group_id = 'com.zxy.android'
upload_version = '0.0.6'
upload_version = '0.0.7'

site_url = 'https://github.com/Sunzxyong/Tiny'
git_url = 'https://github.com/Sunzxyong/Tiny.git'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import com.zxy.tiny.common.CompressResult;
import com.zxy.tiny.common.TinyException;
import com.zxy.tiny.common.UriUtil;
import com.zxy.tiny.core.BitmapCompressor;
import com.zxy.tiny.core.CompressKit;
import com.zxy.tiny.core.FileCompressor;

Expand All @@ -17,8 +16,6 @@
import java.io.IOException;
import java.io.InputStream;

import static com.zxy.tiny.core.BitmapCompressor.compress;

/**
* Created by zhengxiaoyong on 2017/3/13.
*/
Expand Down Expand Up @@ -52,7 +49,7 @@ public BitmapAsFileCallable(Tiny.FileCompressOptions options, boolean withBitmap

@Override
public CompressResult call() throws Exception {
Bitmap bitmap = BitmapCompressor.compress(mBitmap, mCompressOptions, false);
Bitmap bitmap = FileCompressor.shouldKeepSampling(mBitmap, mCompressOptions);
return FileCompressor.compress(bitmap, mCompressOptions, shouldReturnBitmap, false);
}
}
Expand Down Expand Up @@ -98,7 +95,7 @@ public UriAsFileCallable(Tiny.FileCompressOptions options, boolean withBitmap, U

@Override
public CompressResult call() throws Exception {
Bitmap bitmap = new BitmapCompressCallableTasks.UriAsBitmapCallable(mCompressOptions, mUri).call();
Bitmap bitmap = FileCompressor.shouldKeepSampling(mUri,mCompressOptions);
if (mCompressOptions != null && mCompressOptions.overrideSource &&
(UriUtil.isLocalContentUri(mUri) || UriUtil.isLocalFileUri(mUri))) {
mCompressOptions.outfile = UriUtil.getRealPathFromUri(mUri);
Expand Down Expand Up @@ -131,7 +128,7 @@ public ResourceAsFileCallable(Tiny.FileCompressOptions options, boolean withBitm

@Override
public CompressResult call() throws Exception {
Bitmap bitmap = compress(mResId, mCompressOptions, false);
Bitmap bitmap = FileCompressor.shouldKeepSampling(mResId, mCompressOptions);
return FileCompressor.compress(bitmap, mCompressOptions, shouldReturnBitmap, true);
}
}
Expand Down Expand Up @@ -208,7 +205,7 @@ public BatchCompressResult call() throws Exception {
String[] outfilePaths = getBatchOutfilePaths(mCompressOptions, mBitmaps.length);

for (int i = 0; i < mBitmaps.length; i++) {
Bitmap bitmap = mBitmaps[i];
Bitmap bitmap = FileCompressor.shouldKeepSampling(mBitmaps[i], mCompressOptions);
if (mCompressOptions != null && outfilePaths != null && outfilePaths.length == mBitmaps.length)
mCompressOptions.outfile = outfilePaths[i];

Expand Down Expand Up @@ -275,7 +272,7 @@ public BatchCompressResult call() throws Exception {
String[] outfilePaths = getBatchOutfilePaths(mCompressOptions, mResIds.length);

for (int i = 0; i < mResIds.length; i++) {
Bitmap bitmap = compress(mResIds[i], mCompressOptions, false);
Bitmap bitmap = FileCompressor.shouldKeepSampling(mResIds[i],mCompressOptions);
if (mCompressOptions != null && outfilePaths != null && outfilePaths.length == mResIds.length)
mCompressOptions.outfile = outfilePaths[i];

Expand Down
139 changes: 128 additions & 11 deletions tiny/src/main/java/com/zxy/tiny/core/FileCompressor.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
package com.zxy.tiny.core;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;
import android.util.TypedValue;

import com.zxy.libjpegturbo.JpegTurboCompressor;
import com.zxy.tiny.Tiny;
import com.zxy.tiny.common.CompressResult;
import com.zxy.tiny.common.Conditions;
import com.zxy.tiny.common.Logger;
import com.zxy.tiny.common.UriUtil;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
* Created by zhengxiaoyong on 2017/3/13.
Expand All @@ -27,24 +36,16 @@ public static CompressResult compress(byte[] bytes, Tiny.FileCompressOptions opt
options = new Tiny.FileCompressOptions();

CompressResult result = null;
Bitmap bitmap = null;

if (options.isKeepSampling) {
//TODO do you need? compress in the compression process.
BitmapFactory.Options decodeOptions = CompressKit.getDefaultDecodeOptions();
decodeOptions.inPreferredConfig = options.config;
bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length, decodeOptions);
} else {
bitmap = BitmapCompressor.compress(bytes, options, false);
}
Bitmap bitmap = shouldKeepSampling(bytes, options);
Log.e("zxy", "bitmap00:" + bitmap.getWidth() + "," + bitmap.getHeight());
result = compress(bitmap, options, withBitmap, recycle);
return result;
}

public static CompressResult compress(Bitmap bitmap, Tiny.FileCompressOptions options, boolean withBitmap, boolean recycle) {
if (bitmap == null || bitmap.isRecycled())
return null;

Log.e("zxy", "bitmap11:" + bitmap.getWidth() + "," + bitmap.getHeight());
CompressResult result = new CompressResult();

if (options == null)
Expand Down Expand Up @@ -141,4 +142,120 @@ static boolean compress(Bitmap bitmap, String outfile, int quality, Bitmap.Compr
return isSuccess;
}
}

public static Bitmap shouldKeepSampling(byte[] bytes, Tiny.FileCompressOptions options) {
if (bytes == null || bytes.length == 0)
return null;
if (options == null)
options = new Tiny.FileCompressOptions();

Bitmap result = null;
if (options.isKeepSampling) {
//TODO do you need? compress in the compression process.
BitmapFactory.Options decodeOptions = CompressKit.getDefaultDecodeOptions();
decodeOptions.inPreferredConfig = options.config;
result = BitmapFactory.decodeByteArray(bytes, 0, bytes.length, decodeOptions);
} else {
result = BitmapCompressor.compress(bytes, options, false);
}
return result;
}

public static Bitmap shouldKeepSampling(Bitmap bitmap, Tiny.FileCompressOptions options) {
if (bitmap == null || bitmap.isRecycled())
return null;
if (options == null)
options = new Tiny.FileCompressOptions();

Bitmap result = null;
if (options.isKeepSampling) {
result = bitmap;
} else {
result = BitmapCompressor.compress(bitmap, options, false);
}
return result;
}

public static Bitmap shouldKeepSampling(int resId, Tiny.FileCompressOptions options) {
if (options == null)
options = new Tiny.FileCompressOptions();

Bitmap result = null;
if (options.isKeepSampling) {
//for drawable resource,get the original size of resources,without scaling.
InputStream is = null;
Resources resources = Tiny.getInstance().getApplication().getResources();
try {
is = resources.openRawResource(resId, new TypedValue());
BitmapFactory.Options decodeOptions = CompressKit.getDefaultDecodeOptions();
decodeOptions.inPreferredConfig = options.config;
return BitmapFactory.decodeStream(is, null, decodeOptions);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
//ignore...
}
}
}
} else {
result = BitmapCompressor.compress(resId, options, false);
}
return result;
}

public static Bitmap shouldKeepSampling(Uri uri, final Tiny.FileCompressOptions options) {
if (uri == null)
return null;

final Bitmap[] result = {null};
if (UriUtil.isNetworkUri(uri)) {
HttpUrlConnectionFetcher.fetch(uri, new HttpUrlConnectionFetcher.ResponseCallback() {
@Override
public void callback(InputStream is) {
byte[] decodeBytes = CompressKit.transformToByteArray(is);
if (options.isKeepSampling) {
BitmapFactory.Options decodeOptions = CompressKit.getDefaultDecodeOptions();
decodeOptions.inPreferredConfig = options.config;
result[0] = BitmapFactory.decodeByteArray(decodeBytes, 0, decodeBytes.length, decodeOptions);
} else {
result[0] = BitmapCompressor.compress(decodeBytes, options, true);
}
}
});

} else if (UriUtil.isLocalContentUri(uri) || UriUtil.isLocalFileUri(uri)) {
String filePath = UriUtil.getRealPathFromUri(uri);
if (TextUtils.isEmpty(filePath))
return null;
if (Conditions.fileIsExist(filePath) && Conditions.fileCanRead(filePath)) {
FileInputStream fis = null;
File file = new File(filePath);
try {
fis = new FileInputStream(file);
byte[] decodeBytes = CompressKit.transformToByteArray(fis);
if (options.isKeepSampling) {
BitmapFactory.Options decodeOptions = CompressKit.getDefaultDecodeOptions();
decodeOptions.inPreferredConfig = options.config;
result[0] = BitmapFactory.decodeByteArray(decodeBytes, 0, decodeBytes.length, decodeOptions);
} else {
result[0] = BitmapCompressor.compress(decodeBytes, options, true);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (fis != null)
fis.close();
} catch (IOException e) {
//ignore...
}
}
}
}
return result[0];
}
}

0 comments on commit c71eb06

Please sign in to comment.