您的位置:首页 > 编程语言 > Java开发

英文条码创建/打印/java调windows API(方向)2

2016-01-08 16:31 507 查看
http://www.onbarcode.com/products/java_barcode/barcodes/code_128.html#2 -->fomart
 http://jingyan.baidu.com/article/b87fe19ebcb9fd521835689f.html--> 调用window照片查看器向导,设置属性,打印
//调用windows图片查看器,设定图片
                    String fpath="D://barcode//ddww888888.jpeg";
                    try {
                        String cmd = "rundll32 c:\\Windows\\System32\\shimgvw.dll,ImageView_Fullscreen"+" "+"D:\\barcode\\ddww888888.jpeg";
                        Runtime.getRuntime().exec(cmd);
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
/*
 * 文件名:BarcodeImage.java
 * 版权:Copyright by www.taoronge.com
 * 描述:
 * 修改人:ckf
 * 修改时间:2016年1月16日
 * 跟踪单号:
 * 修改单号:
 * 修改内容:
 */

package com.ms.common.contant.util;
    
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.attribute.DocAttributeSet;
import javax.print.attribute.HashDocAttributeSet;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.standard.MediaPrintableArea;
import javax.swing.JFileChooser;

import org.jbarcode.JBarcode;
import org.jbarcode.encode.Code128Encoder;
import org.jbarcode.paint.BaseLineTextPainter;
import org.jbarcode.paint.WidthCodedPainter;
import org.jbarcode.util.ImageUtil;

public class BarcodeImage{
    //生成图片
    public static String code;
    public static String url =new ThreeDes().getProp().getProperty("barcode.url");
    public static String  createBarCode(String filePath, String jbarCode, String format) {
        System.out.println(url);
        code = jbarCode ;
        System.out.println(code);
        String barCodeName = jbarCode + format;
        try {
            BufferedImage bi = null;
            int len = jbarCode.length();
            if (len == 12) {
 
            } else if (len == 13) {
////                int backCode = checkCode(jbarCode);
//                int oldCode = Integer.parseInt(jbarCode.substring(len - 1, len));
//                if (oldCode != backCode) {
//                    return null;
//                }
//                barCode = jbarCode.substring(0, jbarCode.length() - 1);
            }
            JBarcode localJBarcode13 = new JBarcode(Code128Encoder.getInstance(),
                    WidthCodedPainter.getInstance(),
                    BaseLineTextPainter.getInstance());
            //localJBarcode13.setWideRatio(0.1);  
            //localJBarcode13.setXDimension(0.21);
            //localJBarcode13.setBarHeight(40);   
            //localJBarcode13.setXDimension(0.22f);
            //localJBarcode13.setCheckDigit(false);
            bi = localJBarcode13.createBarcode(jbarCode);
            saveToFile(bi, filePath, barCodeName, "jpeg");
     /*       localJBarcode13.setEncoder(EAN13Encoder.getInstance());
            localJBarcode13.setPainter(WideRatioCodedPainter.getInstance());
            localJBarcode13.setTextPainter(EAN13TextPainter.getInstance());
            localJBarcode13.setShowCheckDigit(false);*/
           // printBarCode();
            return filePath + barCodeName;
        } catch (Exception localException) {
            localException.printStackTrace();
            return null;
        }
    }
 
    /**
     * 保存图片文件
     *
     * @param paramBufferedImage
     *            图片流
     * @param filePath
     *            文件路径
     * @param imgName
     *            图片参数
     * @param imgFormat
     *            图片格式
     */
    private static void saveToFile(BufferedImage paramBufferedImage, String filePath,
            String imgName, String imgFormat) {
        try {
            FileOutputStream fileOutputStream = null;
            try {
                File dirFile = new File(filePath);
                if (!dirFile.exists()) {
                    dirFile.mkdirs();
                }
                String imgPath = filePath + "/" + imgName;
                fileOutputStream = new FileOutputStream(imgPath);
            } catch (Exception e) {
                System.out.println("Create Img File Error:" + e.toString());
            }
            ImageUtil.encodeAndWrite(paramBufferedImage, imgFormat,
                    fileOutputStream, 96, 96);
            fileOutputStream.close();
        } catch (Exception localException) {
            System.out.println("Save Img File Error:" + localException);
            localException.printStackTrace();
        }
    }
     
    public static void createAndPrint(String name){
        createBarCode("D:/barcode/", name,".jpeg");
        JFileChooser fileChooser = new JFileChooser(); //创建打印作业    
        File file = new File("D://barcode//"+name+".jpeg"); //获取选择的文件              
        //构建打印请求属性集   
        HashPrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
        //设置打印格式,因为未确定类型,所以选择autosense   
        DocFlavor flavor = DocFlavor.INPUT_STREAM.JPEG;  
        //查找所有的可用的打印服务   
        PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras);   
        //定位默认的打印服务   
        PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
        //条码尺寸  条码位置
        MediaPrintableArea area = new MediaPrintableArea(0, 0, 200,25, MediaPrintableArea.MM);
        pras.add(area);
        //显示打印对话框   
        //PrintService service = ServiceUI.printDialog(null, 200, 200, printService,    
                //defaultService, flavor, pras);   
        //if(service != null){   
            try {   
                file.delete();
                DocPrintJob job = defaultService.createPrintJob(); //创建打印作业   
                FileInputStream fis = new FileInputStream(file); //构造待打印的文件流   
                DocAttributeSet das = new HashDocAttributeSet();   
                Doc doc = new SimpleDoc(fis, flavor, das);   
                //job.print(doc, pras);     
            } catch (Exception e) {   
                e.printStackTrace();   
            }   
        //}        
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: