微信支付二维码生成工具类

3 篇文章 0 订阅

调用此方法可直接通过流在浏览器页面生成二维码并触发微信支付。

QRCodeUtils.encode(String content, String imgPath, OutputStream output, boolean needCompress) 直接上代码

import java.awt.BasicStroke;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Shape;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.OutputStream;
import java.util.Hashtable;
import java.util.Random;

import javax.imageio.ImageIO;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.Result;
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;

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

    private static BufferedImage createImage(String content, String imgPath,
                                             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 (imgPath == null || "".equals(imgPath)) {
            return image;
        }
        // 插入图片    
        QRCodeUtils.insertImage(image, imgPath, needCompress);
        return image;
    }

    /**
     * 插入LOGO  
     *
     * @param source
     *            二维码图片  
     * @param imgPath
     *            LOGO图片地址  
     * @param needCompress
     *            是否压缩  
     * @throws Exception
     */
    private static void insertImage(BufferedImage source, String imgPath,
                                    boolean needCompress) throws Exception {
        File file = new File(imgPath);
        if (!file.exists()) {
            System.err.println(""+imgPath+"   该文件不存在!");
            return;
        }
        Image src = ImageIO.read(new File(imgPath));
        int width = src.getWidth(null);
        int height = src.getHeight(null);
        if (needCompress) { // 压缩LOGO    
            if (width > WIDTH) {
                width = WIDTH;
            }
            if (height > HEIGHT) {
                height = HEIGHT;
            }
            Image image = src.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();
            src = image;
        }
        // 插入LOGO    
        Graphics2D graph = source.createGraphics();
        int x = (QRCODE_SIZE - width) / 2;
        int y = (QRCODE_SIZE - height) / 2;
        graph.drawImage(src, 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();
    }

    /**
     * 生成二维码(内嵌LOGO)  
     *
     * @param content
     *            内容  
     * @param imgPath
     *            LOGO地址  
     * @param destPath
     *            存放目录  
     * @param needCompress
     *            是否压缩LOGO  
     * @throws Exception
     */
    public static String encode(String content, String imgPath, String destPath,
                              boolean needCompress) throws Exception {
        BufferedImage image = QRCodeUtils.createImage(content, imgPath,
                needCompress);
        mkdirs(destPath);
        String file = new Random().nextInt(99999999)+".jpg";
        ImageIO.write(image, FORMAT_NAME, new File(destPath+"/"+file));
        return file;
    }

    /**
     * 当文件夹不存在时,mkdirs会自动创建多层目录,区别于mkdir.(mkdir如果父目录不存在则会抛出异常)  
     * @date 2013-12-11 上午10:16:36  
     * @param destPath 存放目录  
     */
    public static void mkdirs(String destPath) {
        File file =new File(destPath);
        //当文件夹不存在时,mkdirs会自动创建多层目录,区别于mkdir.(mkdir如果父目录不存在则会抛出异常)    
        if (!file.exists() && !file.isDirectory()) {
            file.mkdirs();
        }
    }

    /**
     * 生成二维码(内嵌LOGO)  
     *
     * @param content
     *            内容  
     * @param imgPath
     *            LOGO地址  
     * @param destPath
     *            存储地址  
     * @throws Exception
     */
    public static void encode(String content, String imgPath, String destPath)
            throws Exception {
        QRCodeUtils.encode(content, imgPath, destPath, false);
    }

    /**
     * 生成二维码  
     *
     * @param content
     *            内容  
     * @param destPath
     *            存储地址  
     * @param needCompress
     *            是否压缩LOGO  
     * @throws Exception
     */
    public static void encode(String content, String destPath,
                              boolean needCompress) throws Exception {
        QRCodeUtils.encode(content, null, destPath, needCompress);
    }

    /**
     * 生成二维码  
     *
     * @param content
     *            内容  
     * @param destPath
     *            存储地址  
     * @throws Exception
     */
    public static void encode(String content, String destPath) throws Exception {
        QRCodeUtils.encode(content, null, destPath, false);
    }

    /**
     * 生成二维码(内嵌LOGO)  
     *      content可以作为微信返回的codeURL,调用此方法可直接通过流在浏览器页面
     * 生成二维码并触发微信支付。
     * @param content
     *            内容  
     * @param imgPath
     *            LOGO地址  
     * @param output
     *            输出流  
     * @param needCompress
     *            是否压缩LOGO  
     * @throws Exception
     */
    public static void encode(String content, String imgPath,
                              OutputStream output, boolean needCompress) throws Exception {
        BufferedImage image = QRCodeUtils.createImage(content, imgPath,
                needCompress);
        ImageIO.write(image, FORMAT_NAME, output);
    }

    /**
     * 生成二维码  
     *
     * @param content
     *            内容  
     * @param output
     *            输出流  
     * @throws Exception
     */
    public static void encode(String content, OutputStream output)
            throws Exception {
        QRCodeUtils.encode(content, null, output, false);
    }

    /**
     * 解析二维码  
     *
     * @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;
    }

    /**
     * 解析二维码  
     *
     * @param path
     *            二维码图片地址  
     * @return
     * @throws Exception
     */
    public static String decode(String path) throws Exception {
        return QRCodeUtils.decode(new File(path));
    }

    
}  

### 回答1: 在 Java 中实现微信支付需要使用微信支付 SDK。您可以在微信支付官方网站上下载最新版本的 SDK,并在您的 Java 项目中使用它。 使用微信支付 SDK 的具体步骤如下: 1. 注册微信支付商户并获取商户 ID 和密钥。 2. 下载并导入微信支付 SDK。 3. 创建支付订单并获取支付二维码。 4. 使用扫码支付或者 JSAPI 支付接口调用微信支付。 5. 接收微信支付的异步通知并进行订单状态的更新。 有关微信支付的更多信息,您可以参考微信支付官方文档:https://pay.weixin.qq.com/wiki/doc/api/index.html 希望这些信息对您有帮助! ### 回答2: 要实现微信支付功能,可以使用Java编程语言结合微信支付开放平台提供的API来实现。 首先,需要在微信支付开放平台注册并创建一个应用,获取到应用的AppID、商户号、AppSecret等信息。 接下来,在Java项目中引入微信支付SDK,可以使用第三方库,如"wechatpay-api"来简化开发流程。然后,通过使用SDK提供的相关方法,可以实现以下功能: 1. 获取access_token:通过获取 access_token 接口,获取微信支付接口调用凭证,用于后续接口调用的身份验证。 2. 统一下单:使用统一下单接口,将用户提交的支付信息传给微信支付平台,生成预支付订单,并返回预付款二维码链接或者支付ID。 3. 生成支付链接或二维码:将预付款二维码链接或支付ID返回给前端,通过生成支付链接或者二维码的方式,提供给用户进行支付。 4. 微信支付回调:在用户支付成功后,微信支付平台会异步调用开发者设置的支付结果通知地址,向该地址发送支付结果信息。开发者需要在接收到回调时,验证回调的合法性,并及时处理支付结果。 5. 查询订单状态:通过订单查询接口,可以查询用户支付订单的当前状态,如支付成功、支付失败等。 6. 退款:使用退款接口,可以实现订单退款操作,退还用户支付的金额。 以上是使用Java实现微信支付的基本步骤。在具体实现过程中,还需注意接口调用的参数传递、异常处理、数据加密等问题。为确保支付安全,建议使用HTTPS协议进行数据传输,并加强对接口的签名验证。 ### 回答3: Java 实现微信支付可以通过微信支付开放平台提供的开发工具包来完成。首先,需要在微信支付开放平台注册一个开发者账号,并创建一个应用获取对应的应用ID和应用密钥。 接下来,可以使用Java语言进行开发。首先,需要引入微信支付Java SDK,例如官方提供的weixin-java-pay SDK。通过在项目的pom.xml文件中添加相关依赖,即可将SDK集成到项目中。 然后,通过在代码中进行配置,将应用ID和应用密钥等信息设置到SDK中。可以使用SDK提供的配置类进行设置,例如WxPayConfig。 接下来,可以使用SDK提供的API进行微信支付的相关操作。例如,使用SDK提供的统一下单API可以生成一个支付链接,通过该链接用户可以进行支付操作。可以通过创建对应的请求对象,并调用SDK提供的发送请求的方法来实现。 支付结果通知可以使用SDK提供的回调接口来处理,当支付完成后,微信支付平台会向预先设置的回调URL发送通知。可以在代码中编写对应的处理逻辑,接收并解析微信支付平台发送的通知,校验数据的正确性,并进行相应的业务处理。 最后,可以使用SDK提供的查询订单API来查询订单的支付结果。可以根据订单号或其他查询条件,调用SDK提供的查询订单接口来获取订单的最新支付状态。 通过以上步骤,就可以使用Java实现微信支付功能。需要注意的是,开发过程中需要仔细阅读微信支付开放平台提供的文档和SDK的使用指南,并按照实际需求进行相关配置和操作。同时,为了确保支付过程的安全性,建议使用SSL证书进行加密传输。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程治铭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值