首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用java将图像转换为qr代码?

如何使用java将图像转换为qr代码?
EN

Stack Overflow用户
提问于 2021-12-19 13:47:26
回答 2查看 854关注 0票数 -1

如果我们搜索图像到qr码,有很多网站将图像转换成qr代码。

但是,我如何在java中做到这一点呢?

我使用以下代码将文本转换为qr代码:

代码语言:javascript
复制
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.NotFoundException;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

public class MyQr {

    public static void createQR(String data, String path,
                                String charset, Map hashMap,
                                int height, int width)
        throws WriterException, IOException
    {

        BitMatrix matrix = new MultiFormatWriter().encode(
            new String(data.getBytes(charset), charset),
            BarcodeFormat.QR_CODE, width, height);

        MatrixToImageWriter.writeToFile(
            matrix,
            path.substring(path.lastIndexOf('.') + 1),
            new File(path));
    }

    public static void main(String[] args)
        throws WriterException, IOException,
            NotFoundException
    {

        String data = "TEXT IN QR CODE";

        String path = "image.png";
        String charset = "UTF-8";

        Map<EncodeHintType, ErrorCorrectionLevel> hashMap
            = new HashMap<EncodeHintType,
                        ErrorCorrectionLevel>();

        hashMap.put(EncodeHintType.ERROR_CORRECTION,
                    ErrorCorrectionLevel.L);

        createQR(data, path, charset, hashMap, 200, 200);
    }
}

但是如何在qr代码中编码图像呢?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-12-19 18:04:28

图像太大,无法嵌入到QR代码中(除了微小的图标)。因此,相反,图像被放在一个可公开访问的服务器上,并且图像的URL被嵌入到QR代码中。

因此,您需要访问能够实现此功能的服务器。这可能是您将要实现的更大的部分。

否则,这就是直截了当:

代码语言:javascript
复制
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

public class MyQr {

    /**
     * Uploads the image to a server and returns to image URL.
     * @param imagePath path to the image
     * @return image URL
     */
    public static URL uploadImage(String imagePath)
    {
        // implement a solution to put an image on a public server
        try {
            return new URL("https://yourserver/some_path/image.jpg");
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }
    }

    public static void createQR(String payloadImagePath, String qrCodeImagePath,
                                Map<EncodeHintType, ?> hints, int height, int width)
            throws WriterException, IOException
    {
        URL imageURL = uploadImage(payloadImagePath);

        BitMatrix matrix = new MultiFormatWriter().encode(
                imageURL.toString(), BarcodeFormat.QR_CODE, width, height, hints);

        String imageFormat = qrCodeImagePath.substring(qrCodeImagePath.lastIndexOf('.') + 1);
        MatrixToImageWriter.writeToPath(matrix, imageFormat, Path.of(qrCodeImagePath));
    }

    public static void main(String[] args)
            throws WriterException, IOException
    {
        String payloadImagePath = "embed_this.jpg";
        String qrCodeImagePath = "qrcode.png";

        Map<EncodeHintType, ErrorCorrectionLevel> hints = new HashMap<>();
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);

        createQR(payloadImagePath, qrCodeImagePath, hints, 200, 200);
    }
}

顺便说一句:下面的代码(从您的代码示例中)是没有意义的。充其量,它会创建字符串的副本,这是不必要的。最坏的情况是,它破坏了charset不支持的所有字符。我想你不是故意的。

代码语言:javascript
复制
new String(data.getBytes(charset), charset)
票数 0
EN

Stack Overflow用户

发布于 2021-12-19 14:24:04

我假设您的意思是要在QR代码中添加图像(例如徽标)。

这通常是通过在QR代码之上覆盖一个小图像来完成的,通常是在中间。当一小部分被遮挡时,QR码仍然是可读的,特别是在中央。

您可以使用MatrixToImageWriter.toBufferedImage(matrix)获得包含QR代码映像的BufferedImage,然后使用Java方法覆盖图像。参见,例如,java中的覆盖图像

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70412001

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档