JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Java SE 討論區  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友   
reply to postflat modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 Re:如何修改條碼的大小 [Re:nfty]
ryanpai





發文: 47
積分: 0
於 2007-05-21 17:02 user profilesend a private message to usersend email to ryanpaireply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
這樣可以嗎???

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
 * PrintingExample2.java
 *
 * Created on 2007年5月21日, 下午 4:30
 */
 
//package javatest;
 
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.print.*;
import net.sourceforge.barbecue.*;
 
import javax.swing.*;
import javax.imageio.*;
import javax.print.*;
import javax.print.attribute.*;
import javax.print.attribute.standard.*;
 
/**
 *
 * @author  RYANPAI
 */
public class PrintingExample2 {
    
    // the original Barcode setting 410*80 pixs
    private static final int orgBestWidth = 410;
    private static final int orgBestHigth = 80;
    
    /** Creates a new instance of rintingExample2 */
    public PrintingExample2() {
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        try{
            // TODO code application logic here
 
            // get reScaleJpg
            String jpgFilePath = "C:\\testBarcod.jpg";
            double scaleRate = 0.8;
            String barcodeID = "086240205523";
            
            File reScaleJpg = getReScaleJpg(jpgFilePath,scaleRate,barcodeID);
            
            // get PrintService and jpg to PrintJob
            PrintService service = PrintServiceLookup.lookupDefaultPrintService();
            System.out.println(service.getName());
            
            DocFlavor flavor = DocFlavor.INPUT_STREAM.JPEG;
            DocPrintJob job = service.createPrintJob();
            FileInputStream fis = new FileInputStream(jpgFilePath);
            DocAttributeSet attrs = new HashDocAttributeSet();
            attrs.add(OrientationRequested.PORTRAIT);
            Doc doc = new SimpleDoc(fis, flavor, attrs);
            PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
            job.print(doc, pras);            
            System.out.println("print ok!");
            
            // delete jpg file
            reScaleJpg.delete();
            System.out.println("jpgFile delete ok!");
            
        }
        catch(Exception e) {
            e.printStackTrace();
        }
        
    }
    
    
    
    private static File getReScaleJpg(String jpgFilePath,double scaleRate,String barcodeID) {
        File retFile = null;
        try {
            
            //Barcode barcode  = BarcodeFactory.createCode39("086240205523",true);
            Barcode barcode  = BarcodeFactory.createCode39(barcodeID,true);
            BufferedImage image = new BufferedImage(orgBestWidth, orgBestHigth, BufferedImage.TYPE_BYTE_GRAY);
            Graphics2D g2d1 = (Graphics2D) image.getGraphics();
            g2d1.setColor(Color.WHITE);
            g2d1.fillRect(0,0,orgBestWidth,orgBestHigth);
            barcode .draw(g2d1, 10, 5);
            
            // create reScaleJpg File by scaleRate
            int width = image.getWidth();
            int height = image.getHeight();
            java.awt.Image scaledImage = image.getScaledInstance((int)(width*scaleRate), (int)(height*scaleRate), java.awt.Image.SCALE_DEFAULT);
            BufferedImage bi2 = new BufferedImage((int)(width*scaleRate), (int)(height*scaleRate), BufferedImage.TYPE_BYTE_BINARY);
            Graphics2D g2d2 = (Graphics2D) bi2.getGraphics();
            g2d2.setColor(Color.WHITE);
            g2d2.fillRect(0,0, (int)(width*scaleRate), (int)(height*scaleRate));
            g2d2.drawImage(scaledImage,0,0,null);
            retFile =  new File(jpgFilePath);
            ImageIO.write(bi2, "jpg", retFile);
            
            return retFile;
        }
        catch(Exception e) {
            System.out.println("getReScaleJpg error!");
            e.printStackTrace();
            return retFile;
        }
        
    }
    
}
 


reply to postreply to post
Ryan Pai
話題樹型展開
人氣 標題 作者 字數 發文時間
3977 如何修改條碼的大小 nfty 172 2007-05-16 16:39
3784 Re:如何修改條碼的大小 nfty 471 2007-05-17 16:05
4366 Re:如何修改條碼的大小 ryanpai 2289 2007-05-18 09:57
3591 Re:如何修改條碼的大小 nfty 259 2007-05-19 09:09
3620 Re:如何修改條碼的大小 nfty 2467 2007-05-19 13:37
3583 Re:如何修改條碼的大小 Tand 68 2007-05-19 08:36
3561 Re:如何修改條碼的大小 nfty 1492 2007-05-21 13:35
3850 Re:如何修改條碼的大小 ryanpai 3841 2007-05-21 17:02
3493 Re:如何修改條碼的大小 nfty 66 2007-05-23 11:22
3503 Re:如何修改條碼的大小 Tand 62 2007-05-24 15:41
3588 Re:如何修改條碼的大小 nfty 166 2007-05-24 16:10
» JWorld@TW »  Java SE 討論區

reply to postflat modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8