吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 4419|回复: 13
收起左侧

[Java 原创] java抓取表情包,可以用作微信表情包小程序

  [复制链接]
hunanxingshen 发表于 2020-12-31 11:20
本帖最后由 hunanxingshen 于 2020-12-31 12:09 编辑

首页
image.png

点击图片放大
image.png

左右侧滑
image.png
长按分享保存
image.png


image.png
public void getEmoticonFBQ() throws IOException {
    Map<String, String> map = new HashMap<>();
    map.put("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");
    map.put("Accept-Encoding", "gzip, deflate, br");
    map.put("Accept-Language", "zh-CN,zh;q=0.9");
    map.put("Cookie", "UM_distinctid=1768da8c14a322-04868af06cab59-c791039-384000-1768da8c14bb9b; PHPSESSID=vicpmeug9nmc8vks381l24mcrj; CNZZDATA1260546685=1968502580-1608693375-https%253A%252F%252Fwww.baidu.com%252F%7C1609329890");
    map.put("Upgrade-Insecure-Requests", "1");
    map.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36");
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
    // 图片存储目录及图片名称
    String url_pathYes = "/image/y" + format.format(new Date());
    String url_pathNo = "/image/n" + format.format(new Date());
    int num=1;
    for (int ii = 1; ii <= 50; ii++) {
        log.info("" + ii + "");
        Document document = Jsoup.connect("https://www.fabiaoqing.com/biaoqing/lists/page/" + ii).headers(map).get();
        Elements elements = document.body().select("img");
        log.info("拿到图片准备循环");
        if (num % 5 == 0) {
            num=1;
            url_pathYes = "/image/y" + format.format(new Date());
            url_pathNo = "/image/n" + format.format(new Date());
            getImages(elements,url_pathYes,url_pathNo);
        }else{
            getImages(elements,url_pathYes,url_pathNo);
            num++;
        }
    }
}


private void getImages(Elements elements,String url_pathYes,String url_pathNo) {
    for (int i = 1; i < elements.size(); i++) {
        try {
            String url = elements.get(i).attr("data-original").toString();
            String alt = elements.get(i).attr("alt");
            alt = alt.replace("/", "").replace(" ", "");
            if (StringUtils.isEmpty(alt)) {
                continue;
            }
            log.info("" + i + "条数据:" + alt);
            System.out.println(url);
            URL u = new URL(url);
            URLConnection con = u.openConnection();
            InputStream is = con.getInputStream();

            //转换成字节数组
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] b = new byte[1024];
            int n;
            while ((n = is.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            is.close();
            bos.close();

            //压缩转base64
            byte[] compressionBytes = compressPicForScale(bos.toByteArray(), 15L, alt);
            MultipartFile fileYes = new MockMultipartFile(ContentType.APPLICATION_OCTET_STREAM.toString(), compressionBytes);
            String fileName = saveEmo(url_pathYes, url, alt, fileYes);

            //没有压缩
            MultipartFile fileNo = new MockMultipartFile(ContentType.APPLICATION_OCTET_STREAM.toString(), bos.toByteArray());
            saveEmo(url_pathNo, url, alt, fileNo);

            //保存数据库地址,用户访问
            ImagesEntity im = new ImagesEntity();
            im.setAlt(alt);
            im.setUrl(url_pathNo + "/" + fileName);
            im.setYUrl(url_pathYes + "/" + fileName);
            im.setCreateTime(new Date());
            imagesMapper.saveEntity(im);
            //Thread.sleep(100);
        } catch (Exception e) {
            System.out.println("有异常");
            e.printStackTrace();
        }
    }
}
private String saveEmo(String url_path, String url, String alt, MultipartFile file) throws IOException {
    String staticPath = ClassUtils.getDefaultClassLoader().getResource("static").getPath();
    String[] strArray = url.split("\\.");
    String fileName = alt + "." + strArray[strArray.length - 1];
    //图片保存路径
    String savePath = staticPath + url_path;

    log.info("图片保存地址:" + savePath);
    // 访问路径=静态资源路径+文件目录路径
    String visitPath = "static" + url_path;
    log.info("图片访问uri" + visitPath);
    File saveFile = new File(savePath);
    if (!saveFile.exists()) {
        saveFile.mkdirs();
    }
    file.transferTo(new File(saveFile.getAbsolutePath(), fileName));  //将临时存储的文件移动到真实存储路径下
    return fileName;
}

/**
* 根据指定大小压缩图片
*
* @Param imageBytes  源图片字节数组
* @param desFileSize 指定图片大小,单位kb
* @param imageId     影像编号
* @Return 压缩质量后的图片字节数组
*/
public static byte[] compressPicForScale(byte[] imageBytes, long desFileSize, String imageId) {
    if (imageBytes == null || imageBytes.length <= 0 || imageBytes.length < desFileSize * 1024) {
        return imageBytes;
    }
    long srcSize = imageBytes.length;
    double accuracy = getAccuracy(srcSize / 1024);
    try {
        while (imageBytes.length > desFileSize * 1024) {
            ByteArrayInputStream inputStream = new ByteArrayInputStream(imageBytes);
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream(imageBytes.length);
            Thumbnails.of(inputStream)
                    .scale(accuracy)
                    .outputQuality(accuracy)
                    .toOutputStream(outputStream);
            imageBytes = outputStream.toByteArray();
        }
        log.info("【图片压缩】imageId={} | 图片原大小={}kb | 压缩后大小={}kb", imageId, srcSize / 1024, imageBytes.length / 1024);
    } catch (Exception e) {
        log.error("【图片压缩】msg=图片压缩失败!", e);
    }
    return imageBytes;
}

/**
* 自动调节精度(经验数值)
*
* @param size 源图片大小
* @return 图片压缩质量比
*/
private static double getAccuracy(long size) {
    double accuracy;
    if (size < 900) {
        accuracy = 0.75;
    } else if (size < 2047) {
        accuracy = 0.5;
    } else if (size < 3275) {
        accuracy = 0.4;
    } else {
        accuracy = 0.3;
    }
    return accuracy;
}




免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
joneqm + 1 + 1 用心讨论,共获提升!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

 楼主| hunanxingshen 发表于 2020-12-31 14:12
MrWolfZse 发表于 2020-12-31 14:02
代码没贴全呀,都导入了哪些jar包啊。。。

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import net.coobird.thumbnailator.Thumbnails;
import org.apache.http.HttpRequest;
import org.apache.http.entity.ContentType;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Service;
import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
bennyt 发表于 2020-12-31 11:39
李现在哪 发表于 2020-12-31 13:32
CZQ_Darren 发表于 2020-12-31 13:43
这个还挺实用的
artillery1029 发表于 2020-12-31 13:45
还是java写的,666
BZF187577 发表于 2020-12-31 13:52
有点意思,顶一下
MrWolfZse 发表于 2020-12-31 14:02
代码没贴全呀,都导入了哪些jar包啊。。。
Hegemonism 发表于 2020-12-31 15:28
用心讨论,共获提升!
ma4907758 发表于 2020-12-31 15:50
顶一个,好东西
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-20 08:45

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表