使用google的jar包进行生成二维码,并上传阿里云保存

我们有时候需要把一段地址(url)进行生成一个二维码,那么我们就可以用第三方库(google.zxing jar)来进行生成

导入的jar如下:

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.NotFoundException;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

那么我们就可以用这些库来进行生成二维码图片,

代码如下:

public class Util {
private static final Map<EncodeHintType, ErrorCorrectionLevel> encodeMap = new HashMap<EncodeHintType, ErrorCorrectionLevel>();
    private static final Map<DecodeHintType, ErrorCorrectionLevel> decodeMap=new HashMap<DecodeHintType, ErrorCorrectionLevel>();
    private static final String charset="UTF-8",format="png";
    private static final int size=150;//生成图片的大小
    private static final int size1=200;//生成图片的大小


     /**
     * 生成二维码图片
     * @author huanglei
     * @date 2015-7-10 上午10:26:33
     * @param url 需要生成二维码的地址
     * @param file 生成二维码后保存的地址
     * @throws WriterException
     * @throws IOException
     */
    public static void createQRCode(String url, File file)
            throws WriterException, IOException {
        MatrixToImageWriter.writeToFile(new MultiFormatWriter().encode(
                new String(url.getBytes(charset), charset),
                BarcodeFormat.QR_CODE, size, size, encodeMap), format, file);
    }
}

这样我们就生成二维码了。

但是有的时候我们生成的二维码不是保存在本地的,有时候是需要保存在云上的,这里我们以阿里云为例,生成二维码,并上传到阿里云上。

代码如下:

public class Util {
    private static final Map<EncodeHintType, ErrorCorrectionLevel> encodeMap = new HashMap<EncodeHintType, ErrorCorrectionLevel>();
    private static final Map<DecodeHintType, ErrorCorrectionLevel> decodeMap=new HashMap<DecodeHintType, ErrorCorrectionLevel>();
    private static final String charset="UTF-8",format="png";
    private static final int size=150;//生成二维码图片的大小
    private static final int size1=200;//生成二维码图片的大小


    /**
     * 
     * 【方法功能描述】公共方法-生成二维码图片上传到OSS
     * @author huanglei
     * @date 2018年11月26日 下午5:10:07
     * @version 1.0
     * @param url 生成二维码的URL
     * @param fullpath 上传OSS全路径
     * @return
     * @throws WriterException
     * @throws IOException
     * 【修改描述】
     * @author huanglei
     * @date 2018年11月26日 下午5:10:07
     */
    public static String createQRCode3(String url,String fullpath)
            throws WriterException, IOException {
    	// 定义一个输出流,生成二维码保存在输出流里
    	ByteArrayOutputStream os = new ByteArrayOutputStream();
        MatrixToImageWriter.writeToStream(new MultiFormatWriter().encode(
                new String(url.getBytes(charset), charset),
                BarcodeFormat.QR_CODE, size1, size1, encodeMap), format, os);
        
        // 创建OSSClient实例
        OSSClient ossClient = new OSSClient(
				ConfigInfo.getByProperties("ossjichu.endpoint"),//OSS的地址
				ConfigInfo.getByProperties("ossjichu.accessKeyId"),//相当于账号
				ConfigInfo.getByProperties("ossjichu.accessKeySecret"));//相当于密码
        //OSS下文件夹名称
		String bucket = ConfigInfo.getByProperties("ossjichu.bucket");
		
		byte[] bytes = os.toByteArray();
		
		//上传到OSS是上传字节的形式
		InputStream ips = new ByteArrayInputStream(bytes);
		ossClient.putObject(bucket, fullpath, ips);
		try {
			ips.close();
		} catch (IOException e) {
			Log.error("【生成二维码图片上传到OSS】:上传字节流失败",e);//打印日志
			e.printStackTrace();
		}
		ossClient.shutdown();
        
		// 下载二维码的地址
    	String picUrl = "https://" + bucket + ".oss-cn-szfinance.aliyuncs.com/"+ fullpath;
    	Log.info("【下载二维码的地址:】"+picUrl);//打印日志
        return picUrl;
    }
}

这样我们就可以生成二维码并上传到OSS云上保存了。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值