Java实现彩色二维码

来源:互联网 发布:怎么取消淘宝介入 编辑:程序博客网 时间:2024/05/11 05:24

步骤:

1.导入google 的zxing.jar包
2.核心方法

//生成二维码的类库private static MultiFormatWriter mutiWriter = new MultiFormatWriter();/** * @Description: 生成一个彩色二维码 * @param content 二维码内容 * @param width 宽度 * @param height 高度 * @param path 二维码生成路径 * @author  * @date 2017年11月8日 */public static String encode(String content, int width, int height,String path) {    try {        String fileName = UUID.randomUUID()+".png";        File file = new File(path+"/"+fileName);        ImageIO.write(genBarcode(content, width, height),"png", file);        return fileName;    } catch (Exception e) {        e.printStackTrace();    }    return null;} /** * @Description:生成一个二维码 * @param content  内容 * @param width 宽 * @param height 高 * @return * @throws WriterException * @throws IOException  * @author  * @date 2017年12月25日 */private static BufferedImage genBarcode(String content, int width, int height) throws WriterException, IOException {    Map<EncodeHintType, Object> hint = new HashMap<EncodeHintType, Object>();    hint.put(EncodeHintType.CHARACTER_SET, "utf-8");    hint.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);    // 生成二维码    BitMatrix matrix = mutiWriter.encode(content, BarcodeFormat.QR_CODE,width, height, hint);    int w = matrix.getWidth();      int h = matrix.getHeight();      int[] data = new int[w * h];     boolean flag1=true;    int stopx=0;    for (int y = 0; y < h; y++) {        for (int x = 0; x < w; x++) {            if(matrix.get(x, y)){                if(flag1){                  flag1=false;                }            }else{                if(flag1==false){                    stopx =x;                    break;                }            }        }         if(flag1==false)            break;    }     for (int y = 0; y < h; y++) {          for (int x = 0; x < w; x++) {              if(matrix.get(x, y)){                if((x<stopx)&&(y<stopx)){                    Color color = new Color(231, 144, 56);                    int colorInt = color.getRGB();                    data[y * width + x] =colorInt;                }else{                    int num1 = (int) (50 - (50.0 - 13.0)/ matrix.getHeight()* (y + 1));                    int num2 = (int) (165 - (165.0 - 72.0) / matrix.getHeight()* (y + 1));                    int num3 = (int) (162 - (162.0 - 107.0)/ matrix.getHeight() * (y + 1));                    Color color = new Color(num1, num2, num3);                    int colorInt = color.getRGB();                    data[y * w + x] = colorInt;                }            }else{                data[y * w + x] = -1;//白色            }        }      }      BufferedImage image = new BufferedImage(width, height,            BufferedImage.TYPE_INT_RGB);    image.getRaster().setDataElements(0, 0, width, height, data);    return image;}

效果图
这里写图片描述

阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 冰点脱唇毛 怎么祛唇毛 唇毛怎么去掉 怎样淡化唇毛 女生唇毛怎么去除 怎么去掉唇毛 什么是唇毛 祛唇毛 唇毛怎么除 淡化唇毛小妙招 豆浆 怎样去唇毛女生 唇毛可以拔吗 去唇毛的小妙招 女生怎么去唇毛 唇毛是什么 除唇毛 用醋去除唇毛的方法 淡化唇毛小妙招 怎样去唇毛 女生唇毛重怎么淡化 蜜蜡脱唇毛的危害 如何淡化唇毛 冰点脱唇毛的危害 e光脱唇毛 女生除唇毛 怎么除唇毛 怎么样去除唇毛 女生唇毛怎么办 脱唇毛注意事项 脱唇毛的产品 女性唇毛怎么去除 唇毛多 女生的唇毛怎么去除 脱唇毛效果好吗 女生怎么脱唇毛 女性怎样去唇毛 唇毛脱毛膏 去唇毛一般多少钱 怎样祛除唇毛 怎样减少唇毛 医院脱唇毛多少钱