定位并替换原图中的二维码

pom

		<!--二维码-->
		<dependency>
        <groupId>com.google.zxing</groupId>
        <artifactId>core</artifactId>
        </dependency>
        <dependency>
        <groupId>com.google.zxing</groupId>
        <artifactId>javase</artifactId>
        </dependency>
        <!--二维码-->

代码

package com.sxapp.img.utils;

import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.sanxiang.exception.SXException;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Dirk
 * @Description 二维码
 * @Date Create at 2019-06-28 16:01
 */
public class QrCodeUtil {

    /**
     * 替换原图片里面的二维码
     *
     * @param originImage 原图
     * @param qrImage     要替换的二维码
     * @return 替换后的图片
     * @throws NotFoundException 识别二维码失败
     */
    public static BufferedImage replaceQrCode(BufferedImage originImage, BufferedImage qrImage) throws NotFoundException {

        BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(originImage)));

        Map<DecodeHintType, Object> hints = new HashMap<>(1);
        hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");

        Result result = new MultiFormatReader().decode(binaryBitmap, hints);

        // 定位点的坐标,按照左下、左上、右上顺序
        ResultPoint[] resultPoint = result.getResultPoints();
        float x1 = resultPoint[0].getX();
        float y1 = resultPoint[0].getY();
        float x2 = resultPoint[1].getX();
        float y2 = resultPoint[1].getY();

        // 定位点与起始点的差值
        int deviate = 25;
        // 计算二维码图片边长
        final int length = (int) Math.sqrt(Math.abs(x1 - x2) * Math.abs(x1 - x2) + Math.abs(y1 - y2) * Math.abs(y1 - y2)) + 2 * deviate;
        // 根据二维码定位坐标计算起始坐标
        int x = Math.round(x2) - deviate;
        int y = Math.round(y2) - deviate;

        // 替换二维码图案
        Graphics2D graphics = originImage.createGraphics();
        graphics.drawImage(qrImage, x, y, length, length, null);
        originImage.flush();
        graphics.dispose();

        return originImage;
    }

    /**
     * 生成二维码图片
     *
     * @param content 内容
     * @param width   宽度
     * @param height  高度
     * @param format  格式
     * @return 图像
     */
    public static BufferedImage generateQrCodeImage(String content, int width, int height, String format) {
        HashMap<EncodeHintType, Object> hints = new HashMap<>(4);
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
        hints.put(EncodeHintType.MARGIN, 2);
        try {
            BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            MatrixToImageWriter.writeToStream(bitMatrix, format, byteArrayOutputStream);
            return ImageIO.read(new ByteArrayInputStream(byteArrayOutputStream.toByteArray()));
        } catch (Exception e) {
            e.printStackTrace();
            throw new SXException();
        }
    }
}

java二维码定位获取坐标并替换原来二维码

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值