标签:master java put 功能 type bmp nerd style equals
这两天,在搞归档系统。需要用到Twain协议来驱动打印机。
找了两天,java的twain操作资料真的不多。而且我还是要找直接打印的功能。
后来只能静下心来看类库和源码。最后搞定他。
打印方式分为2种,
1 直接打印:就是程序跑起来,打印机一起跑
2 界面打印:程序跑起来,只是调用打印的默认界面,打印操作在界面完成。
废话不多说了。上攻略:
一 ,首先下载 Twain相关架包
1 直接下载:(我用的方案1。)
https://imagej.nih.gov/ij/plugins/twain-scan.html
2 github:
https://github.com/alex73/mmscomputing/tree/master/src/uk/co/mmscomputing/device
主要是 Twain和Scanner
二 ,准备
架包,该导入的导入。github下载的话,可以直接放java类。
扫描机,该装驱动的装驱动。确保自己打印机能正常工作。
需要打印的材料放好。
我用的是EPSON GT-1500。
三 ,上代码
1 直接打印:
package com.kintech.scanAF.controllerScan; import uk.co.mmscomputing.device.scanner.Scanner; import uk.co.mmscomputing.device.scanner.ScannerIOException; import uk.co.mmscomputing.device.scanner.ScannerIOMetadata; import uk.co.mmscomputing.device.scanner.ScannerListener; import uk.co.mmscomputing.device.twain.TwainScanner; import uk.co.mmscomputing.device.twain.jtwain; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.File; /** * @author Tyler * @date 2019/8/16 */ public class testTwain_Source implements ScannerListener { static int index=0; static String filename=System.getProperty("user.home")+"\\My Documents\\test"; Scanner scanner = Scanner.getDevice(); public testTwain_Source(String[] var1) throws ScannerIOException { TwainScanner twainScanner = new TwainScanner(); twainScanner.addListener(this); jtwain.setScanner(twainScanner); jtwain.getSource().setShowUI(false); jtwain.acquire(twainScanner); } public void update(ScannerIOMetadata.Type var1, ScannerIOMetadata var2) { if (var1.equals(ScannerIOMetadata.ACQUIRED)) { System.out.println("ACQUIRED"); BufferedImage var3 = var2.getImage(); try { ImageIO.write(var3, "png", new File(filename+index+".png")); } catch (Exception var5) { var5.printStackTrace(); } } else if (var1.equals(ScannerIOMetadata.NEGOTIATE)) { System.out.println("NEGOTIATE"); // ScannerDevice var6 = var2.getDevice(); BufferedImage var3 = var2.getImage(); try { ImageIO.write(var3, "png", new File(filename+index+".png")); } catch (Exception var5) { var5.printStackTrace(); } } else if (var1.equals(ScannerIOMetadata.STATECHANGE)) { System.out.println("STATECHANGE"); System.err.println(var2.getStateStr()); if (var2.isFinished()) { System.exit(0); } } else if (var1.equals(ScannerIOMetadata.EXCEPTION)) { System.out.println("EXCEPTION"); var2.getException().printStackTrace(); } } public static void main(String[] var0) throws ScannerIOException { new testTwain_Source(var0); } }
2 界面打印
这个就简单了。(下载的jar里面有)
可以直接写一个class,main中 new TwainExample就可以了。
源码如下:
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler) // package uk.co.mmscomputing.device.twain; import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; import uk.co.mmscomputing.device.scanner.Scanner; import uk.co.mmscomputing.device.scanner.ScannerDevice; import uk.co.mmscomputing.device.scanner.ScannerIOException; import uk.co.mmscomputing.device.scanner.ScannerIOMetadata; import uk.co.mmscomputing.device.scanner.ScannerListener; import uk.co.mmscomputing.device.scanner.ScannerIOMetadata.Type; public class TwainExample implements ScannerListener { static TwainExample app; Scanner scanner = Scanner.getDevice(); public TwainExample(String[] var1) throws ScannerIOException { this.scanner.addListener(this); this.scanner.acquire(); } public void update(Type var1, ScannerIOMetadata var2) { if (var1.equals(ScannerIOMetadata.ACQUIRED)) { BufferedImage var3 = var2.getImage(); System.out.println("Have an image now!"); try { ImageIO.write(var3, "png", new File("mmsc_image.png")); } catch (Exception var5) { var5.printStackTrace(); } } else if (var1.equals(ScannerIOMetadata.NEGOTIATE)) { ScannerDevice var6 = var2.getDevice(); } else if (var1.equals(ScannerIOMetadata.STATECHANGE)) { System.err.println(var2.getStateStr()); if (var2.isFinished()) { System.exit(0); } } else if (var1.equals(ScannerIOMetadata.EXCEPTION)) { var2.getException().printStackTrace(); } } public static void main(String[] var0) { try { app = new TwainExample(var0); } catch (Exception var2) { var2.printStackTrace(); } } }
标签:master java put 功能 type bmp nerd style equals
原文地址:https://www.cnblogs.com/hanjun0612/p/11365582.html