直接上代码:
需要jar 包
1、converter-1.0-SNAPSHOT.jar
2、core-1.0-SNAPSHOT.jar
3、signature-1.0-SNAPSHOT.jar
package com.sino.utils.ofd2img;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.dom4j.DocumentException;
import com.ofdbox.convertor.img.Ofd2Img;
import com.ofdbox.core.OFDReader;
import com.ofdbox.core.model.OFD;
/**
* 测试用例
*/
public class OFDUtilTest {
public static void main(String[] args) throws IOException, DocumentException {
String imgpath = "C:\\ofd票\\011002000511_05690633.ofd";
String ofd2imgPath = "C:\\ofd票\\011002000511_05690633.jpg";
ofd2Img(imgpath, ofd2imgPath);
}
public static String ofd2Img(String path, String ofd2imgPath) {
OFDReader reader = new OFDReader();
reader.getConfig().setValid(false);
try {
OFD ofd1 = reader.read(new File(path));
Ofd2Img ofd2Img = new Ofd2Img();
ofd2Img.getConfig().setDrawBoundary(false);
BufferedImage image1 = ofd2Img.toImage(ofd1.getDocuments().get(0).getPages().get(0), 15);
ImageIO.write(image1, "png", new FileOutputStream(new File(ofd2imgPath)));
} catch (IOException e) {
e.printStackTrace();
System.out.println("ofd -> img 失败");
}
return ofd2imgPath;
}
}