java swing畫圖及和橫豎條碼合成

  public static void main(String[] args) throws IOException { 

    String strInfo="ORD00330725951107";   

    int width =500; 

    int height =800; 

       

    int barCodeWidth=width-140;   

    int barCodeHeight=100;   

     

    int HEIGHT_SPACE = 20;      

               

    //圖片寬度      

    int imageWidth = barCodeWidth;      

    // 圖片高度      

    int imageHeight = barCodeHeight + HEIGHT_SPACE ;     

               

     BufferedImage img=new BufferedImage(width,height,BufferedImage.TYPE_INT_ARGB); 

       

    Graphics2D g = (Graphics2D) img.getGraphics();   

     

     RenderingHints qualityHints = new RenderingHints(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON); 

          qualityHints.put(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY); 

          g.setRenderingHints(qualityHints); 

          

        

          g.setColor(Color.BLACK); 

          g.fillRect(0,0,width,height); 

           

          g.setColor(Color.red); 

          g.fillRect(5, 5, width-10,height-10); 

           

          g.setColor(Color.white); 

          g.fillRect(5, 75, width-10,670); 

           

          g.setColor(Color.white); 

          g.fillRoundRect(width/2, 10, 40, 40, 40, 40); 

          

     

    //    g.fillRect(0, 0, imageWidth, imageHeight);    

       

    Font font = new java.awt.Font("宋體", java.awt.Font.BOLD, 21);    

    g.setColor(Color.BLACK);//設置背景顏色 

  //  g2.setFont(new Font("宋體",Font.BOLD,18)); //設置字體:字體、字號、大小  

     

    Barcode128 barcode128 = new Barcode128();   

               

    FontRenderContext fontRenderContext = g.getFontRenderContext();    

               

    //條形碼(文字)的高度      

    int stringHeight = (int) font.getStringBounds("",fontRenderContext).getHeight();      

    // 圖片橫座標開始位置      

    int startX = 70;      

    // 圖片縱座標開始位置      

    int imageStartY = height-100-70-50;      

    int stringStartY =  height-120-70-8;// 條形碼(文字)開始位置    

               

    int codeWidth = (int) font.getStringBounds(strInfo, fontRenderContext).getWidth();   

    barcode128.setCode(strInfo);   

    java.awt.Image codeImg = barcode128.createAwtImage(Color.black, Color.white);   

     

    g.drawImage(codeImg, startX, imageStartY, barCodeWidth, barCodeHeight, Color.white, null);   

     

    //爲圖片添加條形碼(文字),位置爲條形碼圖片的下部居中   

    AttributedString ats = new AttributedString(strInfo);    

    ats.addAttribute(TextAttribute.FONT, font, 0, strInfo.length());    

    AttributedCharacterIterator iter = ats.getIterator();   

    g.drawString(iter, startX + (barCodeWidth - codeWidth) / 2, stringStartY);    

     

    int w =width/2-(width-height)/2; 

    int h=height/2+80; 

     

    //w / 2 - (w-h)/2, h / 2 

     

    g.drawString(iter, startX + (barCodeWidth - codeWidth) / 2, stringStartY);    

    System.out.println(w+">>"+h); 

     

    g.rotate(Math.toRadians(90),w , h);//y x 

    //g.ro 

    g.drawImage(codeImg, startX, imageStartY, barCodeWidth, barCodeHeight, Color.white, null);   

       

    //爲圖片添加條形碼(文字),位置爲條形碼圖片的下部居中   

    AttributedString ats2 = new AttributedString(strInfo);    

    ats2.addAttribute(TextAttribute.FONT, font, 0, strInfo.length());    

    AttributedCharacterIterator iter2 = ats.getIterator();   

    g.drawString(iter2, startX + (barCodeWidth - codeWidth) / 2, stringStartY);    

    // 設置條形碼(文字)的顏色     

     g.setColor(Color.BLACK);      

    // 繪製條形碼(文字) 

    g.dispose();    

       

    ImageIO.write(img,"PNG",new File("/home/maye/Downloads/output_image.png")); 

  } 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章