根据网址及oss图片logo制作二维码

1、将oss的url转成BufferedImage

//oss的url转成BufferedImage工具类
package com.cxtplatform.promotion.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class OssUrlImgInputStreamTransfer {

    public static ByteArrayInputStream ossUrlTransferStream(String ossUrl) throws IOException {

        HttpURLConnection connection = (HttpURLConnection) new URL(ossUrl).openConnection();
        connection.setReadTimeout(5000);
        connection.setConnectTimeout(5000);
        connection.setRequestMethod("GET");
        InputStream inStream = connection.getInputStream();
        //下面要把inStream变成二进制
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();

        byte[] buffer = new byte[1024];
        int len = -1;
        while((len = inStream.read(buffer)) != -1){
            outStream.write(buffer, 0, len);
        }
        outStream.close();
        inStream.close();
        byte[] base64Str= outStream.toByteArray();//变成了二进制数组
        return new ByteArrayInputStream(base64Str);
    }
}

2、制作二维码工具

package com.cxtplatform.promotion.utils;

/**
 * @program: universities
 * @description: ${description}
 * @author: qtian
 * @create: 2024-02-28 10:00
 **/

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Hashtable;

/**
 * 二维码工具类
 *
 */
public class QRCodeUtil {

    private static final String CHARSET = "utf-8";
    // 二维码尺寸
    private static final int QRCODE_SIZE = 300;
    // LOGO宽度
    private static final int WIDTH = 60;
    // LOGO高度
    private static final int HEIGHT = 60;

    public static BufferedImage createImage(String content, Image logoImage,
                                             boolean needCompress) throws Exception {
        Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
        hints.put(EncodeHintType.MARGIN, 1);
        BitMatrix bitMatrix = new MultiFormatWriter().encode(content,
                BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE, hints);
        int width = bitMatrix.getWidth();
        int height = bitMatrix.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, bitMatrix.get(x, y) ? 0xFF000000
                        : 0xFFFFFFFF);
            }
        }
        if (ObjectUtils.isEmpty(logoImage)) {
            return image;
        }
        // 插入图片
        QRCodeUtil.insertImage(image, logoImage, needCompress);
        return image;
    }

    /**
     * 插入LOGO
     *
     * @param source
     *            二维码图片
     * @param logoImage
     *            LOGO图片
     * @param needCompress
     *            是否压缩
     * @throws Exception
     */
    private static void insertImage(BufferedImage source, Image logoImage,
                                    boolean needCompress) throws Exception {

        int width = logoImage.getWidth(null);
        int height = logoImage.getHeight(null);
        if (needCompress) { // 压缩LOGO
            if (width > WIDTH) {
                width = WIDTH;
            }
            if (height > HEIGHT) {
                height = HEIGHT;
            }
            Image image = logoImage.getScaledInstance(width, height,
                    Image.SCALE_SMOOTH);
            BufferedImage tag = new BufferedImage(width, height,
                    BufferedImage.TYPE_INT_RGB);
            Graphics g = tag.getGraphics();
            g.drawImage(image, 0, 0, null); // 绘制缩小后的图
            g.dispose();
            logoImage = image;
        }
        // 插入LOGO
        Graphics2D graph = source.createGraphics();
        int x = (QRCODE_SIZE - width) / 2;
        int y = (QRCODE_SIZE - height) / 2;
        graph.drawImage(logoImage, x, y, width, height, null);
        Shape shape = new RoundRectangle2D.Float(x, y, width, width, 6, 6);
        graph.setStroke(new BasicStroke(3f));
        graph.draw(shape);
        graph.dispose();
    }


    /**
     * 解析二维码
     *
     * @param file
     *            二维码图片
     * @return
     * @throws Exception
     */
    public static String decode(File file) throws Exception {
        BufferedImage image;
        image = ImageIO.read(file);
        if (image == null) {
            return null;
        }
        BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(
                image);
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
        Result result;
        Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();
        hints.put(DecodeHintType.CHARACTER_SET, CHARSET);
        result = new MultiFormatReader().decode(bitmap, hints);
        String resultStr = result.getText();
        return resultStr;
    }

}

3、根据网址及oss图片logo制作二维码

 //获取logo的oss地址
                    String logoMedia = liveCode.getLogoMedia();
                    ByteArrayInputStream in = OssUrlImgInputStreamTransfer.ossUrlTransferStream(logoMedia);
                        BufferedImage image = ImageIO.read(in);     //将in作为输入流,读取图片存入image中,

                        // 将 BufferedImage 转换为 Image 对象
                        Image image1 = Toolkit.getDefaultToolkit().createImage(image.getSource());

                        BufferedImage bufferedImage = QRCodeUtil.createImage(qrCodeUrl, image1, true);
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 如果你想将图片转换成二维码并上传到OSS服务器,你可以使用以下步骤: 1. 安装必要的依赖库。你需要安装 `pyqrcode` 和 `boto3` 库来生成二维码和连接到OSS服务器。你可以使用以下命令安装这些库: ``` pip install pyqrcode boto3 ``` 2. 生成二维码。你可以使用 `pyqrcode` 库来生成二维码。例如,你可以使用以下代码生成一个包含文本 `hello world` 的二维码: ```python import pyqrcode qr = pyqrcode.create('hello world') ``` 3. 将二维码保存到文件中。你可以使用 `save` 方法将二维码保存到文件中。例如,你可以使用以下代码将二维码保存为 `qrcode.png` 文件: ```python qr.png('qrcode.png', scale=8) ``` 4. 连接到OSS服务器。你可以使用 `boto3` 库来连接到OSS服务器。首先,你需要在OSS控制台中获取你的访问密钥和密钥ID。然后,你可以使用以下代码连接到OSS服务器: ```python import boto3 # Replace with your access key and secret key ACCESS_KEY = 'your access key' SECRET_KEY = 'your secret key' # Connect to the OSS server oss = boto3.client( 's3', aws_access_key_id=ACCESS_KEY, aws_secret_access_key=SECRET_KEY, ) ``` 5. 上传文件。你可以使用 `upload_file` 方法将 ### 回答2: 要实现将图片转成二维码并上传到OSS服务器,可以通过以下步骤完成。 首先,我们需要选择一个合适的开发工具来实现图片二维码功能。常用的工具有Python的qrcode库,Java的zxing(Zebra Crossing)库等。 接下来,我们需要下载并安装选定的开发工具,并在项目中导入相关的库和依赖项。 然后,我们需要编写代码来实现图片二维码的功能。代码中需要将图片加载进内存,并使用二维码库将其转换成二维码。转换完成后,可以将二维码保存为图片文件,或者直接将其存储在内存中。 接下来,我们需要连接到OSS服务器,并在其中创建一个存储图片的目录。需要使用连接OSS服务器的相关信息,例如访问URL、Access Key等。 然后,我们需要将生成的二维码上传至OSS服务器。可以使用选定的开发工具提供的上传功能,将二维码文件上传至指定目录。 最后,我们可以在OSS服务器上验证上传是否成功,通过访问相应的URL来获取二维码图片。 综上所述,要实现图片转成二维码并上传到OSS服务器,需要选择合适的开发工具,编写代码来实现图片二维码功能,连接OSS服务器并上传二维码文件。只要按照以上步骤进行操作,就可以将图片转成二维码并存储到OSS服务器上。 ### 回答3: 要将图片转成二维码并上传到OSS服务器,需要以下步骤: 首先,需要安装一个二维码生成库。可以选择使用Python的qrcode库来生成二维码。可以使用pip命令安装: ``` pip install qrcode ``` 然后,导入qrcode库,并使用库的make函数生成二维码。将要转换的图片作为输入参数传递给make函数,生成二维码对象。 ```python import qrcode image_path = 'image.jpg' qr = qrcode.make(image_path) ``` 接下来,需要将生成的二维码保存为图片文件。可以使用二维码对象的save函数保存为图片。 ```python qr.save('qrcode.png') ``` 生成的二维码图片可以进行进一步操作,如调整大小、添加logo等。然后,将生成的二维码图片上传到阿里云OSS服务器,可以使用阿里云的Python SDK。 首先,需要安装阿里云的Python SDK。可以使用pip命令安装。 ``` pip install aliyun-python-sdk-core-v3 ``` 然后,导入阿里云的Python SDK,并进行相关的配置。 ```python from aliyunsdkcore import client access_key_id = 'your_access_key_id' access_secret = 'your_access_secret' endpoint = 'your_oss_endpoint' bucket = 'your_bucket_name' clt = client.AcsClient(access_key_id, access_secret, endpoint) ``` 接下来,使用OSS的BucketManager对象上传二维码图片。 ```python from aliyunsdkcore.request import CommonRequest request = CommonRequest() request.set_accept_format('json') request.set_domain(endpoint) request.set_method('PUT') request.set_protocol_type('https') request.set_bucket_name(bucket) request.set_key('qrcode.png') # 设置上传后的文件名 request.set_input_file('qrcode.png') # 设置要上传的文件路径 response = clt.do_action(request) ``` 以上就是将图片转成二维码并上传到OSS服务器的步骤。注意替换代码中的相应配置信息,确保代码正常运行。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值