阿里OSS上載自定義二維碼

private static final MatrixToImageConfig DEFAULT_CONFIG = new MatrixToImageConfig();html


/** * 生成圖像 * * @throws WriterException * @throws IOException */ public static void generateQr() throws WriterException, IOException { String content = "http://baidu.com?mobile=123";// json.toJSONString();// // 內容 int width = 200; // 圖像寬度 int height = 200; // 圖像高度 String format = "png";// 圖像類型 Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>(); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);// 生成矩陣 BufferedImage image = toBufferedImage(bitMatrix, DEFAULT_CONFIG);//輸出圖 ByteArrayOutputStream os = new ByteArrayOutputStream(); ImageIO.write(image,format, os); //寫入IO流 InputStream is = new ByteArrayInputStream(os.toByteArray()); //轉換成引入流導向到oss qrAppCode(is,os.size()); System.out.println("輸出成功."); }
public static void qrAppCode(InputStream imp,long size) {
        String savePath = "upload/jimw/";//ServiceConfig.getSavePath();
        String bucketName = "psppictest";//ServiceConfig.getBucketName();
        String endPonit="";
        String accessKeyId = "accessKeyId";//ServiceConfig.getAccessKeyId();
        String accessKeySecret = "accessKeySecret";//ServiceConfig.getAccessKeySecret();
        String endpoint = "http://oss-cn-xx.aliyuncs.com/";//ServiceConfig.getEndpoint();
        String filePath = savePath + "test" + ".jpg";
        // 初始化一個OSSClient
        OSSClient client = new OSSClient(endpoint, accessKeyId, accessKeySecret);
        /*
         * key是Object的名字;meta是用戶對該object的描述,由一系列name-value對組成;data是Object的數據
         */
        try {
            InputStream is =imp;
         
            ObjectMetadata meta = new ObjectMetadata();
            // 必須設置ContentLength
            meta.setContentType("image/jpeg");

            meta.setCacheControl("max-age=8640000");
            long file_size = size;
            meta.setContentLength(file_size);
            // 上傳Object.
           
            PutObjectResult result = client.putObject(bucketName, filePath, is, meta);
            log.info(result.getETag());
            // 訪問路徑

            Iterable<String> splitter = Splitter.on("http://").omitEmptyStrings().split(endpoint); //ServiceConfig.getEndpoint() does
            for (String string : splitter) {
                endPonit = string;
            }
            String path ="http://" + bucketName + "." + endPonit + "/" + filePath;
            System.out.println("換取二維碼成功:http://" + bucketName + "." + endPonit + "/" + filePath);
            log.info("換取二維碼成功:http://" + bucketName + "." + endPonit + "/" + filePath);
         
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        
    }

/**
	 * As {@link #toBufferedImage(BitMatrix)}, but allows customization of the
	 * output.
	 *
	 * @param matrix
	 *            {@link BitMatrix} to write
	 * @param config
	 *            output configuration
	 * @return {@link BufferedImage} representation of the input
	 */
	public static BufferedImage toBufferedImage(BitMatrix matrix, MatrixToImageConfig config) {
		int width = matrix.getWidth();
		int height = matrix.getHeight();
		BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_BINARY);
		int onColor = config.getPixelOnColor();
		int offColor = config.getPixelOffColor();
		for (int x = 0; x < width; x++) {
			for (int y = 0; y < height; y++) {
				image.setRGB(x, y, matrix.get(x, y) ? onColor : offColor);
			}
		}
		return image;
	}
 
 

  

 

參考文檔:java

https://help.aliyun.com/document_detail/32013.html?spm=5176.doc31839.6.673.COPAcGjson

參考包:google

google.zxingspa

com.aliyun.openservicecode

相關文章
相關標籤/搜索