• JAVA 生成二维码


    https://files.cnblogs.com/files/xuejianxiyang/core.jar.rar  导入jar包

    //标准
    boolean bl = false;

    //生成内容
    String text =url;

    //二维码宽度和高度
    int width = 300;
    int height = 300;

    //二维码的图片格式
    String format = "jpg";
    Hashtable hints = new Hashtable();

    //内容所使用编码
    hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
    BitMatrix bitMatrix;

    try {
    bitMatrix = new MultiFormatWriter().encode(text,
    BarcodeFormat.QR_CODE, width, height, hints);

    Desktop dtp = (Desktop) Executions.getCurrent().getDesktop();
    String realPath = dtp.getSession().getWebApp().getRealPath("/file/download/");
    //生成二维码
    File outputFile = new File(realPath+news.getId()+".jpg");
    try {
    MatrixToImageWriter.writeToFile(bitMatrix, format, outputFile);
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    bl = true;
    showPic="/file/download/"+news.getId()+".jpg";

    } catch (WriterException e) {
    // TODO Auto-generated catch block
    bl=false;
    e.printStackTrace();
    }

    if(bl){
    System.out.println("成功");
    }
    else{
    System.out.println("失败");
    }

    /**
    * 生成二维码 
    *
    */
    public class MatrixToImageWriter {

    private static final int BLACK = 0xFF000000;
    private static final int WHITE = 0xFFFFFFFF;

    public static BufferedImage toBufferedImage(BitMatrix matrix) {
    int[] rec = matrix.getEnclosingRectangle();
    int resWidth = rec[2] + 1;
    int resHeight = rec[3] + 1;
    BitMatrix resMatrix = new BitMatrix(resWidth, resHeight);
    resMatrix.clear();
    for (int i = 0; i < resWidth; i++) {
    for (int j = 0; j < resHeight; j++) {
    if (matrix.get(i + rec[0], j + rec[1])) {
    resMatrix.set(i, j);
    }
    }
    }
    int width = resMatrix.getWidth();
    int height = resMatrix.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, resMatrix.get(x, y) == true ? 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 void writeToOutputStream(BitMatrix matrix, String format,OutputStream os) throws IOException{
    BufferedImage image = toBufferedImage(matrix);
    if (!ImageIO.write(image, format, os)) {
    throw new IOException("Could not write an image of format " + format + " to " + os);
    }
    }

    public static void writeToOutputStream(String value, String format,OutputStream os,int width,int height)
    throws IOException{
    Map<EncodeHintType,String> hints = new Hashtable<>();
    try {
    BitMatrix bitMatrix = new MultiFormatWriter().encode(value,BarcodeFormat.QR_CODE, width, height, hints);
    writeToOutputStream(bitMatrix,format,os);
    } catch (WriterException e) {
    e.printStackTrace();
    }
    }

    /**
    * 获得需要生成图片的格式类型
    * @param pathname 路径和图片的名称与格式
    * @return 图片的格式 如:jpg,gif 等等,如果字符串不正确则返回NULL
    */
    public static String getImageFormat(String pathname){
    int num = pathname.lastIndexOf("."); //获得字符串中最后一次出现【.】的下标数
    if(num > -1){
    return pathname.substring(num+1);
    }else{
    return null;
    }
    }

    /**
    * 将字符串生成二维码,并存放到指定文件夹中
    * @param str 需要生成二维码的字符串
    * @param pathname 存放二维码图片的路径(路径中包含生成二维码图片的名称及图片格式)
    * @param width 二维码图片的宽度
    * @param height 二维码图片的高度
    * @return 成功返回true 失败返回false  
    */
    public static boolean foundToImage(String str,String pathname,int width,int height){

    try {

    //二维码的图片格式
    String format = getImageFormat(pathname);
    Map<EncodeHintType,Object> hints = new Hashtable<>();
    //内容所使用编码
    hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
    hints.put(EncodeHintType.MARGIN,1);
    BitMatrix bitMatrix = new MultiFormatWriter().encode(str,BarcodeFormat.QR_CODE, width, height, hints);
    //生成二维码
    File outputFile = new File(pathname);
    writeToFile(bitMatrix, format, outputFile);

    return true;

    } catch (Exception e) {
    e.printStackTrace();
    return false;
    }

    }

    public static void main(String[] args){
    String urlstr = "http://192.168.1.147:8090";
    String pathname = "E:"+File.separator+"erweima.jpg";
    System.out.println(foundToImage(urlstr, pathname, 300, 300));
    }

    60F40508-3411-4A16-8EF7-CBC7C6D0E659 From:https://www.cnblogs.com/xuejianxiyang/p/9143541.html

  • 相关阅读:
    许可管理工具
    浅谈MapControl控件和PageLayoutControl控件
    通过Bresenham算法实现完成矢量线性多边形向栅格数据的转化
    四叉树算法原理与实现
    OC系列foundation Kit基础-NSNumber
    OC系列foundation Kit基础-NSdictionary
    OC系列foundation Kit基础-NSMutableArray
    OC系列foundation Kit基础-NSArray
    OC系列foundation Kit基础-NSMutableString
    OC系列foundation Kit基础-NSString
  • 原文地址:https://www.cnblogs.com/xuejianxiyang/p/9143541.html
Copyright © 2020-2023  润新知