最近在做公司应用的时候,那个分享到朋友圈,有的图片一直分享不过去(好吧,我只想说之前不是我做的),
后来去去看微信官方看了下,分享到朋友圈的图片的缩略图只能是32kb一下的图片,怎么办?当然得压缩了,压缩的话
,就得考虑是否失真啊,清晰度啊等等,好了 这些都是题外话,下面直接上代码吧
public static void shareToPengyou(Context ct, String imageKey,
String piccapText) {
String path = LocalImageMaintain.getInstance().getLocalImageDir()
+ imageKey + IMAGE_SUFFIX;
try {
Bitmap bmp = ImageTools.getBitmap(path, 2);
WXImageObject imgObj = new WXImageObject(bmp);
WXMediaMessage msg = new WXMediaMessage();
msg.mediaObject = imgObj;
Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, 120, 120, true);
msg.thumbData = bmpToByteArray(thumbBmp, true); // 设置缩略图
int tempSize = msg.thumbData.length;
int temp = 1;
while (tempSize > (32 * 1024)) {
temp ++;
bmp = ImageTools.getBitmap(path, (2 * temp));
msg.mediaObject = imgObj;
thumbBmp = Bitmap.createScaledBitmap(bmp, 120, 120, true);
msg.thumbData = bmpToByteArray(thumbBmp, true); // 设置缩略图
tempSize = msg.thumbData.length;
}
SendMessageToWX.Req req = new SendMessageToWX.Req();
req.transaction = buildTransaction("img");
req.message = msg;
req.scene = SendMessageToWX.Req.WXSceneTimeline;
IWXAPI api = WXAPIFactory.createWXAPI(ct, WXEntryActivity.APP_ID);
api.sendReq(req);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//图片压缩
public static byte[] bmpToByteArray(final Bitmap bmp,
final boolean needRecycle) {
ByteArrayOutputStream output = new ByteArrayOutputStream();
bmp.compress(CompressFormat.PNG, 80, output);
if (needRecycle) {
bmp.recycle();
}
byte[] result = output.toByteArray();
try {
output.close();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
//简单的一个工具类,用来等比压缩的,size指的是压缩为原来的1/size
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.util.FloatMath;
public class ImageTools {
// 指定大小�d入�D片
public static Bitmap getBitmap(String path, int size) {
Options op = new Options();
op.inSampleSize = size;
Bitmap bt = BitmapFactory.decodeFile(path, op);
return bt;
}
// 按��高�嚎s�d入�D片
public static Bitmap getBitmap(String path, int width, int heigh) {
Options op = new Options();
op.inJustDecodeBounds = true;
Bitmap bt = BitmapFactory.decodeFile(path, op);
int xScale = op.outWidth / width;
int yScale = op.outHeight / heigh;
op.inSampleSize = xScale > yScale ? xScale : yScale;
op.inJustDecodeBounds = false;
bt = BitmapFactory.decodeFile(path, op);
return bt;
}
// 按��高�嚎s�d入�D片方法2
public static Bitmap getBitmap2(String imageFilePath, int displayWidth,
int displayHeight) {
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmapOptions.inJustDecodeBounds = true;
Bitmap bmp = BitmapFactory.decodeFile(imageFilePath, bitmapOptions);
// 编码后bitmap的宽高,bitmap除以屏幕宽度得到压缩比
int widthRatio = (int) FloatMath.ceil(bitmapOptions.outWidth
/ (float) displayWidth);
int heightRatio = (int) FloatMath.ceil(bitmapOptions.outHeight
/ (float) displayHeight);
if (widthRatio > 1 && heightRatio > 1) {
if (widthRatio > heightRatio) {
// 压缩到原来的(1/widthRatios)
bitmapOptions.inSampleSize = widthRatio;
} else {
bitmapOptions.inSampleSize = heightRatio;
}
}
bitmapOptions.inJustDecodeBounds = false;
bmp = BitmapFactory.decodeFile(imageFilePath, bitmapOptions);
return bmp;
}
}