• 阿里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

     

     

     

  • 相关阅读:
    JeeSite信息化快速开发平台
    Redis操作使用规范
    使用Redis配置JAVA_环境
    BZOJ 1029 [JSOI2007]建筑抢修 已更新
    COJ 2108 Day7-例1
    COJ 2105 submatrix
    COJ 2106 road
    更新一下缺省源
    hiho #1055 : 刷油漆
    COJ 0047 20702最大乘积
  • 原文地址:https://www.cnblogs.com/jimw/p/8241460.html
Copyright © 2020-2023  润新知