1. 程式人生 > >生成二維碼並直接上傳到七牛上

生成二維碼並直接上傳到七牛上

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 "";
}
}

相關推薦

生成直接

package com.casic.util; import com.casic.anfangyun.expo.model.ExpoApply; import com.casic.anfangyun.expo.model.ExpoAudienceRegister; impo

Android 把url生成貼到給定的底圖

主要是用到了com.google.zxing jar包生成二維碼的功能,這個jar包需要自己接下載  直接上程式碼 public static Bitmap CreateBinaryCodeImageByUrl(String url,Bitmap bottomImg,int dra

window環境下利用MFC生成顯示在對話方塊

要達成如題的目的,首先要對以下知識做一些瞭解: 1.MFC建立對話方塊工程以及在對話方塊下的空間操作。 2.MFC載入圖片以及顯示圖片。 3.二維碼的相關知識。 首先,瞭解bmp圖片格式。 BMP格式介紹 BITMAP結構體引數介紹 然後,瞭解什麼是二維碼以及實現過程。 二維碼介

JAVA------19.生成插入logo

nds java .sh extend span exist dbi content height 1.jar包 鏈接:http://pan.baidu.com/s/1jHDD6sA 密碼:luhs import java.awt.BasicStroke; import

Thinkphp3.2版本結合phpqrcode生成提供下載

com value inf PQ png 容錯 AI mini eve 說明:這篇文章主要為大家詳細介紹了Thinkphp3.2.3整合phpqrcode生成帶二維碼的實現方法並提供圖像下載,感興趣的小夥伴們可以參考一下 緣由:Thinkphp中沒有二維碼相關的庫,因此我們

zxing生成以流式到頁面

@RequestMapping(“/makeQrCode”) public void madeQrCode(HttpServletResponse res,String content,Integer width,Integer height) throws IOException{

react-native 生成截圖儲存的功能實現

近期專案開發需要,需要更加不同使用者生成隨機的二維碼,並實現儲存該二維碼(包含二維碼周邊的背景圖,類似支付寶的紅包二維碼圖片)的功能。在網上查詢相關元件,開始使用的是react-native-qrcode元件,截圖功能使用的是react-native-view-shot元件,儲存圖片則是使用的rea

如何生成統計掃描次數,展示頁面

引入了 phpqrcode 外掛來生成自定義二維碼 //建立空二維碼 唯一標識code public function mkCode() { // 引入外掛 include '/static/phpqrcode/phpqrcode.php'

在vue中利用vue-qr外掛動態生成嵌入LOGO

  收到需求要生成二維碼的時候剛進專案組不久,接觸vue也才一兩個星期,還處於懵逼狀態。   本小白的第一反應就是百度二維碼的生成方法,網上有很多大神給出解決方案,最開始本小白以為是在後臺生成圖片然後傳到前臺頁面,後來發現可以直接在前端用js生成,網上查到的大部分都是用jquery.qrcode.js配合u

生成指定地址跳轉

生成二維碼並指定地址跳轉 /* * 功能:生成二維碼 * @param string $qr_data 手機掃描後要跳轉的網址 * @param string $qr_level 預設糾錯比例 分為L、M、Q、H四個等級,H代表最高糾錯能力 * @param string $qr_size

不儲存批量生成壓縮zip下載

對於我一個前端來說,做這個也是醉了;搜了好多都是對已有的檔案打包下載的,這裡自己組合了下;先看程式碼吧! Controller方法(springMVC) /** * 二維碼 * * @param code * @param request * @par

微信小程式生成,並且進行參。

微信小程式生成二維碼第一步.首先我們需要去拿到Token值 第一步是一個比較常規的操作,未遇到大的問題。第二步.拿著Token進行換取二維碼    這一不是很關鍵的地方,騰訊用token換取二維碼的方法有三組介面A,B,C    我們推薦生成並使用小程式碼,它具有更好的辨識度

java生成解析(QRCode方式)

準備工作  下載jar http://www.swetake.com/qrcode/java/qr_java.html https://zh.osdn.net/projects/qrcode/downloads/28391/qrcode.zip/ jar下載完畢後新建專案

libqrencode生成儲存成bmp圖片

#include "qrencode.h" #define QT_OUT_FILE "/root/Application/QRBMP/qrpay.bmp" // Prescaler (number of pixels in bmp file for ea

【微信開發】SpringMVC將url生成圖片直接展示在頁面上

利用google的開源包zxing生成二維碼 第一步:maven專案的zxing依賴 1 <!-- google zxing 生成二維碼 --> 2 <dependency> 3 <groupId>com.goog

微信裡生成長按儲存

               <div class="Qrcode"><div id="qr_container"></div></div>                               createQr();//

QRCode生成帶有logo圖片

最近專案需要生成帶logo的二維碼供前臺下載,在網上找了幾篇文章,整合了一下。 首先需要的是QRCode.jar,我專案是直接maven依賴 <!-- Qrcode二維碼 --> <dependency> <

呼叫微信小程式API生成 轉base64存資料庫

public class GeneralTokenUtil {     private static final String appid = "";     private static final String grant_type = "";     // 獲取to

libqrencode學習筆記(): 用libqrencode靜態庫庫生成儲存為BMP圖片

#include <stdio.h> #include <Windows.h> #include <qrencode.h> #pragma comment(lib,"libqrencode.lib") int main() { const char * QRTEXT

視訊轉換生成,掃直接播放,線上製作,一鍵

如何視訊生成二維碼,掃描就能看到視訊?最好不要有廣告?  先和大家說一下原理。二維碼本身可以儲存資訊,例如儲存視訊的URL。當掃描二維碼的時候,手機會先獲取二維碼儲存的URL,然後自動在瀏覽器中開啟URL,所以就能看到視訊。微信和QQ內建的都是QQ瀏覽器,所以原理也是一樣的