阿里OSS上载自定义二维码

private static final MatrixToImageConfig DEFAULT_CONFIG = new MatrixToImageConfig();


/** * 生成图像 * * @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;
	}

  

 

参考文档:

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

参考包:

google.zxing

com.aliyun.openservice

 

 

 

原文地址:https://www.cnblogs.com/jimw/p/8241460.html