Android分享微信小程序给微信好友,封面bitmap的处理

2020-09-16  本文已影响0人  hao_developer

\color{red}{~~bitmap不能大于128k~~}

image.png
第一步:url转换为bitmap
val imgPath = "https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=1332951276,3206293713&fm=26&gp=0.jpg"
object : AsyncTask<Void, Void, Bitmap>(){

    override fun doInBackground(vararg params: Void?): Bitmap {
   //调用下面的方法getBitmap,传入线上图片url路径
     return ImageLoadUtils.getBitmap("上下文",imgPath)
     }

     override fun onPostExecute(result: Bitmap?) {
           super.onPostExecute(result)
           result?.let {
                  PublicTools.tools.shareToWxProudct("上下文","分享小程序页面路径及拼接参数",it)
            }
       }

  }.execute()
//url转bitmap
public static Bitmap getBitmap(String urlpath) {
        Bitmap bm = null;
        try {
            URL iconUrl = new URL(urlpath);
            URLConnection conn = iconUrl.openConnection();
            HttpURLConnection http = (HttpURLConnection) conn;

            int length = http.getContentLength();

            conn.connect();
            // 获得图像的字符流
            InputStream is = conn.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is, length);
            bm = BitmapFactory.decodeStream(bis);
            bis.close();
            is.close();// 关闭流
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return bm;
    }

第二步:先对bitmap进行处理(drawWXMiniBitmap),再判断得到的bitmap是否大于128k(createBitmapThumbnail),
\color{red}{bitmap转二进制不能大于128k}

 public static Bitmap drawWXMiniBitmap(Bitmap bitmap, int width, int height) {
        Bitmap mBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        // 用这个Bitmap生成一个Canvas,然后canvas就会把内容绘制到上面这个bitmap中
        Canvas mCanvas = new Canvas(mBitmap);
        // 绘制画笔
        Paint mPicturePaint = new Paint();
        // 绘制背景图片
        mCanvas.drawBitmap(mBitmap, 0.0f, 0.0f, mPicturePaint);
        // 绘制图片的宽、高
        int width_head = bitmap.getWidth();
        int height_head = bitmap.getHeight();
        // 绘制图片--保证其在水平方向居中
        mCanvas.drawBitmap(bitmap, (width - width_head) / 2, (height - height_head) / 2,
                mPicturePaint);
        // 保存绘图为本地图片
        mCanvas.save();
        mCanvas.restore();
        return mBitmap;
    }
/**
     * Bitmap转换成byte[]并且进行压缩,压缩到不大于maxkb
     *
     * @param bitmap
     * @param maxKb
     * @return
     */
    public static byte[] createBitmapThumbnail(Bitmap bitmap, int maxKb) {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, output);
        int options = 100;
        while (output.toByteArray().length > maxKb && options != 10) {
            output.reset();
            bitmap.compress(Bitmap.CompressFormat.JPEG, options, output);
            options -= 10;
        }
        return output.toByteArray();
    }

第三步:申请配置微信开放平台的appid,再到微信公众平台拿小程序的原始id,最后拉起微信,并分享出去
\color{red}{1:appid 微信开放平台的appid}
\color{red}{2:miniProgramObj.miniprogramType分享小程序的版本:正式版:0,测试版:1,体验版:2}
\color{red}{3:miniProgramObj.userName 小程序原始id}
\color{red}{4:miniProgramObj.path 分享小程序的页面路径及参数拼接}

val appId = MyParms.APP_ID // 填应用AppId
val api = WXAPIFactory.createWXAPI(this, appId)

val miniProgramObj = WXMiniProgramObject()
miniProgramObj.webpageUrl = " " // 兼容低版本的网页链接
// 正式版:0,测试版:1,体验版:2
miniProgramObj.miniprogramType = WXMiniProgramObject.MINIPTOGRAM_TYPE_RELEASE
miniProgramObj.userName = MyParms.WX_SMALL_PROGRAM // 小程序原始id
//小程序页面路径;对于小游戏,可以只传入 query 部分,来实现传参效果,如:传入 "?foo=bar"
 miniProgramObj.path = "pages/cardDetail/cardDetail?pcid=$cardId"

val msg = WXMediaMessage(miniProgramObj)
msg.title = "您赞,就是赞" // 小程序消息title
msg.description = "您赞,就是赞" // 小程序消息desc
val bitmap = BitmapFactory.decodeResource(resources, R.mipmap.icon)
 if (null == bitmap) {
     toast("分享失败")
     return
}
//bitmap.height*5/4 ,bitmap.height
val newBitmap = ImageLoadUtils.drawWXMiniBitmap(bitmap,bitmap.width ,bitmap.height)
//此参数不能为空
msg.thumbData = ImageLoadUtils.createBitmapThumbnail(newBitmap,128)  // 小程序消息封面图片,小于128k
bitmap.recycle()
val req = SendMessageToWX.Req()
req.transaction = "您赞,就是赞"//buildTransaction("miniProgram")
req.message = msg
req.scene = SendMessageToWX.Req.WXSceneSession // 目前只支持会话

api.sendReq(req)
上一篇下一篇

猜你喜欢

热点阅读