【问题标题】:How to convert image to qr code using java?如何使用java将图像转换为二维码?
【发布时间】:2022-01-21 11:46:53
【问题描述】:

如果我们搜索image to qr code,有很多在线将图像转换为二维码的网站。

但是我怎样才能在 java 中做到这一点呢?
我正在使用以下代码将文本转换为二维码:-

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);
    }
}

但是如何在二维码中编码图像呢?

【问题讨论】:

  • 我假设您的意思是创建一个包含图像链接的二维码。将图像保存到 URL。将 URL 转换为二维码。

标签: java image qr-code


【解决方案1】:

图片太大,无法嵌入二维码(小图标除外)。因此,图像被放置在可公开访问的服务器上,并且图像的 URL 嵌入在 QR 码中。

因此,您需要访问可以实现此目的的服务器。这可能是您将要实现的大部分内容。

否则,它是直截了当的:

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 不支持的所有字符。我不认为你想要任何一个。

new String(data.getBytes(charset), charset)

【讨论】:

    【解决方案2】:

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

    这通常是通过在 QR 码顶部(通常在中心)覆盖一个小图像来完成的。当一小部分被遮挡时,二维码仍然可以读取,尤其是在中心。

    您可以使用MatrixToImageWriter.toBufferedImage(matrix)获取包含二维码图像的BufferedImage,然后使用Java图像方法叠加图像。例如,请参阅overlay images in java

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-23
      • 1970-01-01
      • 2015-08-15
      • 1970-01-01
      • 1970-01-01
      • 2014-06-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多