定位並替換原圖中的二維碼

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二維碼定位獲取座標並替換原來二維碼

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章