本地图片/网络图片、二维码合成海报,上传七牛云

本文介绍了如何将本地图片、网络图片和二维码合成为海报,并使用七牛云进行上传存储。内容包括添加pom依赖、创建实体类、海报及二维码工具类的实现,以及七牛云上传工具类的详细步骤。参考了相关资源链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、pom文件添加依赖

<dependency>
	<groupId>com.google.zxing</groupId>
	<artifactId>core</artifactId>
	<version>3.2.1</version>
</dependency>
<dependency>
	<groupId>com.google.zxing</groupId>
	<artifactId>javase</artifactId>
	<version>3.2.1</version>
</dependency>

<dependency>
	<groupId>commons-codec</groupId>
	<artifactId>commons-codec</artifactId>
	<version>1.10</version>
</dependency>

2、实体类

public class ImgVo {
    private String picUrl; //图片
    private String qrcodeUrl; //在线详情链接  例如:https://www.baidu.com/
    private String qrCodeImgUrl; //读取本地二维码

    public String getPicUrl() {
        return picUrl;
    }

    public void setPicUrl(String picUrl) {
        this.picUrl = picUrl;
    }

    public String getQrcodeUrl() {
        return qrcodeUrl;
    }

    public void setQrcodeUrl(String qrcodeUrl) {
        this.qrcodeUrl = qrcodeUrl;
    }

    public String getQrCodeImgUrl() {
        return qrCodeImgUrl;
    }

    public void setQrCodeImgUrl(String qrCodeImgUrl) {
        this.qrCodeImgUrl = qrCodeImgUrl;
    }
}

3、海报工具类

public class PosterUtils {
    public static String generatePoster(ImgVo vo){
        Date dt = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
        String qrCode = QrCodeUtil.createQrCode(vo.getQrcodeUrl(), sdf.format(dt) +".jpg");
        String url = null;
        try {
            //InputStream bgImg =new ImgUtils().getImageStream(vo.getPicUrl());//网络图片
            InputStream bgImg = new FileInputStream(vo.getPicUrl());//本地图片
            vo.setQrCodeImgUrl(qrCode);
            url = new ImgUtils().drawImage(bgImg,vo);
        }catch (Exception e){e.printStackTrace();}
        return url;
    }
}

4、图片合成工具类(图片和二维码合成一张图片)

public class ImgUtils {

    /**
     * 合成图片
     * @return url
     * @throws IOException
     */
    public static String drawImage(InputStream bgImgUrl, ImgVo vo) throws IOException {
        BufferedImage posterBufImage = ImageIO.read(bgImgUrl);
        Graphics2D posterBufImageGraphics = posterBufImage.createGraphics();

        InputStream qrCodeImgUrl = new FileInputStream(vo.getQrCodeImgUrl());
        BufferedImage qrCodeImage = ImageIO.read(qrCodeImgUrl);

        posterBufImageGraphics.drawImage(qrCodeImage, (posterBufImage.getWidth() - qrCodeImage.getWidth()), posterBufImage.getHeight()-qrCodeImage.getHeight(), qrCodeImage.getWidth(), qrCodeImage.getHeight(), null);
        posterBufImageGraphics.dispose();

        ByteArrayOutputStream bs = new ByteArrayOutputStream();
        ImageOutputStream imgOut = ImageIO.createImageOutputStream(bs);
        ImageIO.write(posterBufImage, "png", imgOut);
        String localUrl = byte2image(bs.toByteArray());
        // 上传到服务器上
        String url = QiniuUtil.upload(localUrl);
        return url;
    }

    public static String byte2image(byte[] data){
        Date dt = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
        String fileName = sdf.format(dt) +".jpg";
        String path = FileSystemView.getFileSystemView().getHomeDirectory() + File.separator + "testQrcode\\"+fileName;
        if(data.length<3||path.equals("")) return "";//判断输入的byte是否为空
        try{
            FileImageOutputStream imageOutput = new FileImageOutputStream(new File(path));//打开输入流
            imageOutput.write(data, 0, data.length);//将byte写入硬盘
            imageOutput.close();
            return path;
        } catch(Exception ex) {
            ex.printStackTrace();
        }
        return path;
    }

    /**
     * 获取网络图片流
     */
    public InputStream getImageStream(String url) {
        try {
            HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
            connection.setReadTimeout(5000);
            connection.setConnectTimeout(5000);
            connection.setRequestMethod("GET");
            if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                InputStream inputStream = connection.getInputStream();
                return inputStream;
            }
        } catch (IOException e) {
            System.out.println("获取网络图片出现异常,图片路径为:" + url);
            e.printStackTrace();
        }
        return null;
    }

}

5、二维码工具类

public class QrCodeUtil {
    /**
     * 生成二维码
     * @param url 参数路径
     * @param fileName 文件名称
     * @return
     */
    public static String createQrCode(String url,String fileName) {
        //生成的地址
        String path = FileSystemView.getFileSystemView().getHomeDirectory() + File.separator + "testQrcode";
        try {
            Map<EncodeHintType, String> hints = new HashMap<>();
            hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
            BitMatrix bitMatrix = new MultiFormatWriter().encode(url, BarcodeFormat.QR_CODE, 220, 220, hints);
            File file = new File(path, fileName);
            if (file.exists() || ((file.getParentFile().exists() || file.getParentFile().mkdirs()) && file.createNewFile())) {
                writeToFile(bitMatrix, "png", file);
                return file.getPath();

            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    static void writeToFile(BitMatrix matrix, String format, File file) throws IOException {
        BufferedImage image = toBufferedImage(matrix);
        if (!ImageIO.write(image, format, file)) {
            throw new IOException("Could not write an image of format " + format + " to " + file);
        }
    }

    private static final int BLACK = 0xFF000000;
    private static final int WHITE = 0xFFFFFFFF;

    /**
     * 生成内容
     * @param matrix
     * @return
     */
    private static BufferedImage toBufferedImage(BitMatrix matrix) {
        int width = matrix.getWidth();
        int height = matrix.getHeight();
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
            }
        }
        return image;
    }


}

6、七牛云上传工具类

public class QiniuUtil {

    // 设置需要操作的账号的AK和SK
    private static final String ACCESS_KEY = "";
    private static final String SECRET_KEY = "";

    // 空间名
    private static final String bucket = "";

    public static String upload(String localFilePath) {
        Configuration configuration = new Configuration(Zone.zone2());
        UploadManager manager = new UploadManager(configuration);
        Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
        String upToken = auth.uploadToken(bucket);
        String[] arr = localFilePath.split("\\\\");
        String fileName = arr[arr.length-1].split("\\.")[0];
        String key = "turing"+fileName;
        try {
            manager.put(localFilePath, key, upToken);
            String posterPicture = key;
            return posterPicture;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}

参考:

七牛云全国各地服务器上传地址:http://www.xyok.net/share/735.html  

图片+二维码合成图片:https://my.oschina.net/u/2426590/blog/3027726

七牛云上传图片云存储:https://blog.youkuaiyun.com/tangzekk/article/details/88094130

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值