美文网首页安卓开发实用集
裁剪微信分享缩略图片(长缩略图取中裁剪)

裁剪微信分享缩略图片(长缩略图取中裁剪)

作者: 隰有荷 | 来源:发表于2017-11-22 19:17 被阅读44次

    有时分享出去的图片如果过长,就会导致缩略图的正方形图片呈现的效果是被压缩的,一般才用居中裁剪的方式,将裁剪后的图作为缩略图即可

    本文后面将举微信分享到朋友圈和好友到例子,在分享出去到图片中,若为长图,则截取中间部分作为缩略图标,先放实现的代码。例子作为代码的复习笔记使用

    实现图片居中裁剪代码如下:

    //其中bitmap为老的Bitmap对象,将会被裁剪
    
    Bitmap withSizeBitmap = null;//该withSizeBitmap是取过长宽的
    Bitmap thumbBmp = null;//将上面的withSizeBitmap再次压缩否分享出去                  
    if (bitmap.getWidth() < bitmap.getHeight()) {//如果是竖长图或正方形
         //居中裁剪创建一个带有宽高的bitmap
         withSizeBitmap = Bitmap.createBitmap(bitmap, 0, ((bitmap.getHeight() - bitmap.getWidth()) / 2), bitmap.getWidth(), bitmap.getWidth());
         //压缩
         thumbBmp = Bitmap.createScaledBitmap(withSizeBitmap, WX_THUMB_SIZE, WX_THUMB_SIZE, true);                 
    }else if (bitmap.getWidth() >= bitmap.getHeight()){//如果是横长图
         withSizeBitmap = Bitmap.createBitmap(bitmap, ((bitmap.getWidth() - bitmap.getHeight()) / 2), 0, bitmap.getHeight(), bitmap.getHeight());
         thumbBmp = Bitmap.createScaledBitmap(withSizeBitmap, WX_THUMB_SIZE, WX_THUMB_SIZE, true);
    }
    

    分享朋友圈的方法:

    /**
    * 分享朋友圈
    * @param url             链接
    * @param title           标题
    * @param content         内容简介
    * @param imgPath         缩略图图片地址(本地)
    * @param titleOrContent  分享出去的文字是标题还是简介
    */
    
    public static void shareMoments(final String url, final String title, final String 
           content, final String imgPath, final boolean titleOrContent) {
    
         final int IMAGE_SIZE = 32768;
         new Thread(new Runnable() {
             @Override
             public void run() {
                 WXWebpageObject textObj = new WXWebpageObject();
                 textObj.webpageUrl = url;      
                 WXMediaMessage msg = new WXMediaMessage();         
                 if (titleOrContent){//标题还是描述都传过来,但是需要哪个根据此字段决定,
                    msg.title = title;
                 }else {
                    msg.title = content;
                 }
                final int WX_THUMB_SIZE = 120;
                try {
                    Bitmap bitmap = BitmapFactory.decodeFile(imgPath);
                    ByteArrayOutputStream output = new ByteArrayOutputStream();         
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, output);
                    int options = 100;
                    while (output.toByteArray().length > IMAGE_SIZE && options != 10) {
                        output.reset(); //清空baos
                        bitmap.compress(Bitmap.CompressFormat.JPEG, options, output);//这里压缩options%,把压缩后的数据存放到baos中
                        options -= 10;
                    }
                    //这就是上面的裁剪的方法
                    Bitmap withSizeBitmap = null;
                    Bitmap thumbBmp = null;
                    if (bitmap.getWidth() < bitmap.getHeight()) {
                        withSizeBitmap = Bitmap.createBitmap(bitmap, 0, ((bitmap.getHeight() - bitmap.getWidth()) / 2), bitmap.getWidth(), bitmap.getWidth());
                        thumbBmp = Bitmap.createScaledBitmap(withSizeBitmap, WX_THUMB_SIZE, WX_THUMB_SIZE, true);
                    }else if (bitmap.getWidth() >= bitmap.getHeight()){
                        //创建一个带有宽高的bitmap
                        withSizeBitmap = Bitmap.createBitmap(bitmap, ((bitmap.getWidth() - bitmap.getHeight()) / 2), 0, bitmap.getHeight(), bitmap.getHeight());
                        //压缩
                        thumbBmp = Bitmap.createScaledBitmap(withSizeBitmap, WX_THUMB_SIZE, WX_THUMB_SIZE, true);
                    }
                    msg.thumbData = WXUtil.bmpToByteArray(thumbBmp, true);
    
                    bitmap.recycle();
                    withSizeBitmap.recycle();
                    thumbBmp.recycle();
                    msg.mediaObject = textObj;
                    SendMessageToWX.Req req = new SendMessageToWX.Req()
    
                    // req.transaction = content;
                    req.message = msg;
                    req.scene = SendMessageToWX.Req.WXSceneTimeline;
                    api.sendReq(req);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
    

    分享到微信好友到方法:

    /**
     * 分享微信好友
     * @param url     网址
     * @param title   标题
     * @param content 简介 
     * @param imgPath 图片地址(本地)
    */
    
    public static void shareWX(final String url, final String title, final String 
           content, final String imgPath) {
    
          final int IMAGE_SIZE = 32768;
          new Thread(new Runnable() {
                  @Override
                  public void run() {
                       final WXWebpageObject textObj = new WXWebpageObject();
                       textObj.webpageUrl = url;
                       final WXMediaMessage msg = new WXMediaMessage();
                       msg.title = title;//标题
                       msg.description = content;//描述
                       final int WX_THUMB_SIZE = 120;
                       //开始把图片路径转成bitmap
                       try {
                            Bitmap bitmap = BitmapFactory.decodeFile(imgPath);
                            ByteArrayOutputStream output = new ByteArrayOutputStream();
                            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, output);
                            int options = 100;
                            while (output.toByteArray().length > IMAGE_SIZE && options != 10) {//后面(options != 10)的条件判断必须加(vivo x7不加不行)
                                output.reset(); //清空baos
                                bitmap.compress(Bitmap.CompressFormat.JPEG, options, output);//这里压缩options%,把压缩后的数据存放到baos中
                                      options -= 10;
                            }
                            Bitmap withSizeBitmap = null;
                            Bitmap thumbBmp = null;
                            if (bitmap.getWidth() < bitmap.getHeight()) {//竖图
                                 withSizeBitmap = Bitmap.createBitmap(bitmap, 0, ((bitmap.getHeight() - bitmap.getWidth()) / 2), bitmap.getWidth(), bitmap.getWidth());
                                 thumbBmp = Bitmap.createScale dBitmap(withSizeBitmap, WX_THUMB_SIZE, WX_THUMB_SIZE, true);
                            }else if (bitmap.getWidth() >= bitmap.getHeight()){//横图
                                 withSizeBitmap = Bitmap.createBitmap(bitmap, ((bitmap.getWidth() - bitmap.getHeight()) / 2), 0, bitmap.getHeight(), bitmap.getHeight());
                                 thumbBmp = Bitmap.createScaledBitmap(withSizeBitmap, WX_THUMB_SIZE, WX_THUMB_SIZE, true);
                            }
                            msg.thumbData = WXUtil.bmpToByteArray(thumbBmp, true);
    
                            //回收
                            bitmap.recycle();
                            withSizeBitmap.recycle();
                            thumbBmp.recycle();
                            //bitmap到此结束
    
                            msg.mediaObject = textObj;
    
                            SendMessageToWX.Req req = new SendMessageToWX.Req();
                            req.transaction = title;
                            req.message = msg;
                            req.scene = SendMessageToWX.Req.WXSceneSession;
    
                            api.sendReq(req);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
           }
        }).start();
     }
    

    结束。

    相关文章

      网友评论

        本文标题:裁剪微信分享缩略图片(长缩略图取中裁剪)

        本文链接:https://www.haomeiwen.com/subject/tadovxtx.html