微信分享QQ分享微博分享時的圖片壓縮實現
阿新 • • 發佈:2019-02-08
在進行第三方平臺分享時都需要對分享的圖片進行一些壓縮才能正常分享出去 圖片分享不能太大 縮圖不超過32k 。而壓縮又需要耗時不能再主執行緒中進行,所以進行了簡單的封裝一次性返回所需要的圖片資料.
ShareImageCompress
分享到微信微博或者QQ時圖片壓縮並生成縮圖
針對微信分享時需要壓縮圖片到較小的圖片尺寸 並且需要設定縮略32k的限制.
針對業務場景新增圖片壓縮前的拼接貼二維碼或者一些其他操作.
ShareImageDataUtil.create() 可以傳入網路圖片地址 也可以傳入本地bitmap 如果同時傳遞了這兩個引數 會直接使用bitmap而不會再去下載網路圖片
1.分享圖片模式
ShareImageData shareImageData = ShareImageDataUtil.create("http://pic35.photophoto.cn/20150522/0034034855249416_b.jpg"
, MediaType.MEDIA_TYPE_IMG,null);
new ContentManager<ShareImageData>(MainActivity.this).getShareContent(shareImageData, new Observer<ShareImageData>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(ShareImageData shareImageData) {
Log.e("shareimage","size:" + shareImageData.getPosterImageData().length);
// shareImageData 裡包含主圖byte[]資料(shareImageData.getPosterImageData()) 和縮圖byte[](shareImageData.getPosterThumbData())
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
Log.e("shareimage","complete");
}
},new GlideImageDownload());
2.分享連結模式
ShareImageData shareImageData = ShareImageDataUtil.create("http://pic35.photophoto.cn/20150522/0034034855249416_b.jpg"
, MediaType.MEDIA_TYPE_WEB,null);
new ContentManager<ShareImageData>(MainActivity.this).getShareContent(shareImageData, new Observer<ShareImageData>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(ShareImageData shareImageData) {
Log.e("shareimage","size:" + shareImageData.getPosterImageData().length);
// 包含縮圖byte[] (shareImageData.getWebPageThumbData())
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
Log.e("shareimage","complete");
}
},new GlideImageDownload());
3.微信小程式的圖片壓縮
ShareImageData shareImageData = ShareImageDataUtil.create("http://pic35.photophoto.cn/20150522/0034034855249416_b.jpg"
, MediaType.MEDIA_TYPE_MINI_APP,null);
new ContentManager<ShareImageData>(MainActivity.this).getShareContent(shareImageData, new Observer<ShareImageData>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(ShareImageData shareImageData) {
Log.e("shareimage","size:" + shareImageData.getPosterImageData().length);
// 包含小程式圖片資料 byte[] (shareImageData.getWebPageThumbData())
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
Log.e("shareimage","complete");
}
},new GlideImageDownload());
4.圖片附加操作
private Observable<Bitmap> dowloadExtraObservable = Observable.create(emitter -> {
Bitmap bitmap = Glide.with(getApplicationContext())
.asBitmap()
.load("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1515476758278&di=1a65b298738b7467faff309b7df72fb3&imgtype=0&src=http%3A%2F%2Fscimg.jb51.net%2Fallimg%2F161207%2F102-16120H243090-L.jpg")
.submit(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
.get();
Log.e(TAG, "onNext");
emitter.onNext(bitmap);
emitter.onComplete();
});
IShareImageDownLoad shareImageDownLoad = ImageDownLoadFactory.factory(GlideImageDownload.class);
new ContentManager<ShareImageData>(this).getShareContent(ShareImageDataUtil.create("http://f8.topitme.com/8/25/80/1125177570eea80258o.jpg",MediaType.MEDIA_TYPE_IMG,null), new Observer<ShareImageData>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(ShareImageData shareImageData) {
Log.e(TAG,"mediatype:" + shareImageData.getMediaType() + " postersize:" + shareImageData.getPosterImageData().length);
}
@Override
public void onError(Throwable e) {
Log.e(TAG, "onError");
e.printStackTrace();
}
@Override
public void onComplete() {
Log.e(TAG, "onComplete");
}
},shareImageDownLoad, dowloadExtraObservable, ((posterBitmap, bitmap2) -> {
Bitmap finalBitmap = Bitmap.createBitmap(posterBitmap.getWidth(), posterBitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(finalBitmap);
canvas.drawBitmap(posterBitmap, 0, 0, null);
canvas.drawBitmap(bitmap2, 0, 0, null);
return finalBitmap;
}));
github 地址