我是靠谱客的博主 活泼马里奥,最近开发中收集的这篇文章主要介绍生成二维码并直接上传到七牛上,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

package com.casic.util;
import com.casic.anfangyun.expo.model.ExpoApply;
import com.casic.anfangyun.expo.model.ExpoAudienceRegister;
import com.google.gson.Gson;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;


import javax.imageio.ImageIO;


import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.io.IOException;
import java.io.InputStream;
import java.awt.image.BufferedImage;
/**生成二维码*/
public class QRCode {
private static final int BLACK = 0xFF000000;
private static final int WHITE = 0xFFFFFFFF;


private QRCode() {
}


public static BufferedImage toBufferedImage(BitMatrix matrix) {
int width = matrix.getWidth();
int height = matrix.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, matrix.get(x, y) ? BLACK : WHITE);
}
}
return image;
}


public static void writeToFile(BitMatrix matrix, String format, File file) throws IOException {
BufferedImage image = toBufferedImage(matrix);
if (!ImageIO.write(image, format, file)) {
throw new IOException("Could not write an image of format "+ format + " to " + file);
}
}
/**生成二维码*/
public static String  uploadImg(ExpoAudienceRegister expoAudienceRegister){    //传入的参数
   Configuration cfg = new Configuration(Zone.zone0());
   //...其他参数参考类注释
   UploadManager uploadManager = new UploadManager(cfg);
   //...生成上传凭证,然后准备上传
   String accessKey = "=========";
   String secretKey = "==========";
   String bucket = ConfigInno.bucket;
   //默认不指定key的情况下,以文件内容的hash值作为文件名
   String key = null;
   try {   
   Auth auth = Auth.create(accessKey, secretKey);
   String upToken = auth.uploadToken(bucket);
   try {
// 生成二维码
// String uploadImgUrl = PropertiesUtils.getProperty("uploadImgUrl")+"image/carousel";
// String imgName =System.currentTimeMillis()+".png";
// File outputFile = new File(uploadImgUrl,imgName);
// QRCode.writeToFile(bitMatrix, "png", outputFile);
    MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
Map hints = new HashMap();
// 内容所使用编码
hints.put(EncodeHintType.CHARACTER_SET, "utf8");
//所传信息
String QRCodeString=expoAudienceRegister.getId()+"";
if(null != expoAudienceRegister.getFrontuserid() && null != expoAudienceRegister.getBbsmoduleid()){
QRCodeString=expoAudienceRegister.getFrontuserid()+"_"+expoAudienceRegister.getBbsmoduleid();
}
BitMatrix bitMatrix = multiFormatWriter.encode(QRCodeString+"", BarcodeFormat.QR_CODE, 200, 200, hints);
BufferedImage bufferedImage = QRCode.toBufferedImage(bitMatrix);
ByteArrayOutputStream os = new ByteArrayOutputStream();  
ImageIO.write(bufferedImage, "jpg", os);  
InputStream is = new ByteArrayInputStream(os.toByteArray());
       Response response = uploadManager.put(is,key,upToken,null, null);
       //解析上传成功的结果
       DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
      // System.out.println(putRet.key+"二维码");
       return putRet.key;
   } catch (QiniuException ex) {
       Response r = ex.response;
       System.err.println(r.toString());
       try {
           System.err.println(r.bodyString());
       } catch (QiniuException ex2) {
       }
   }
   } catch (Exception ex) {
   }
   return "";
}
}

最后

以上就是活泼马里奥为你收集整理的生成二维码并直接上传到七牛上的全部内容,希望文章能够帮你解决生成二维码并直接上传到七牛上所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(46)

评论列表共有 0 条评论

立即
投稿
返回
顶部