问题描述
我正在尝试将彩色图像或非图像 PDF 文件转换为多页 tiff 文件。为此,我编写了一些代码,但此代码将 PDF 文件转换为黑白的 tiff 文件。我需要 tiff 文件应该是彩色的。那么我应该进行哪些更改才能获得彩色 tiff 文件。
public void btnConvertToTiffActionPerformed(ActionEvent e) throws InterruptedException {
final double FAX_RESOLUTION = 200.0;
final double PRINTER_RESOLUTION = 300.0;
// This compression type may be wpecific to JAI ImageIO Tools
final String COMPRESSION_TYPE_GROUP4FAX = "CCITT T.6";
Iterator<ImageWriter> iterator = ImageIO.getImageWritersByFormatName("tiff");
if (!iterator.hasNext()) {
System.out.println(
"ImageIO missing required plug-in to write TIFF files. " +
"You can download the JAI ImageIO Tools from: " +
"https://jai-imageio.dev.java.net/");
return;
}
boolean foundCompressionType = false;
for(String type : iterator.next().getDefaultWriteParam().getCompressionTypes()) {
if (COMPRESSION_TYPE_GROUP4FAX.equals(type)) {
foundCompressionType = true;
break;
}
}
if (!foundCompressionType) {
System.out.println(
"TIFF ImageIO plug-in does not support Group 4 Fax " +
"compression type ("+COMPRESSION_TYPE_GROUP4FAX+")");
return;
}
Document document = new Document();
try {
document.setFile(file.getAbsolutePath());
} catch (PDFException ex) {
System.out.println("Error parsing PDF document " + ex);
} catch (PDFSecurityException ex) {
System.out.println("Error encryption not supported " + ex);
} catch (FileNotFoundException ex) {
System.out.println("Error file not found " + ex);
} catch (IOException ex) {
System.out.println("Error handling PDF document " + ex);
}
try {
// save page caputres to file.
File file = new File("D:\\pdfbox\\imageCapture.tif");
ImageOutputStream ios = ImageIO.createImageOutputStream(file);
ImageWriter writer = ImageIO.getImageWritersByFormatName("tiff").next();
writer.setOutput(ios);
// Paint each pages content to an image and write the image to file
for (int i = 0; i < document.getNumberOfPages(); i++) {
final double targetDPI = PRINTER_RESOLUTION;
float scale = 1.0f;
float rotation = 0f;
// Given no initial zooming,calculate our natural DPI when
// printed to standard US Letter paper
PDimension size = document.getPageDimension(i,rotation,scale);
double dpi = Math.sqrt((size.getWidth()*size.getWidth()) +
(size.getHeight()*size.getHeight()) ) /
Math.sqrt((8.5*8.5)+(11*11));
// Calculate scale required to achieve at least our target DPI
if (dpi < (targetDPI-0.1)) {
scale = (float) (targetDPI / dpi);
size = document.getPageDimension(i,scale);
}
int pageWidth = (int) size.getWidth();
int pageHeight = (int) size.getHeight();
int[] cmap = new int[] { 0xFF000000,0xFFFFFFFF };
IndexColorModel cm = new IndexColorModel(8,cmap.length,cmap,true,Transparency.OPAQUE,DataBuffer.TYPE_BYTE);
BufferedImage image = new BufferedImage(
pageWidth,pageHeight,BufferedImage.TYPE_BYTE_BINARY,cm);
Graphics g = image.createGraphics();
document.paintPage(
i,g,GraphicsRenderingHints.PRINT,Page.BOUNDARY_CROPBOX,scale);
g.dispose();
// capture the page image to file
IIOImage img = new IIOImage(image,null,null);
ImageWriteParam param = writer.getDefaultWriteParam();
param.setCompressionMode(param.MODE_EXPLICIT);
param.setCompressionType(COMPRESSION_TYPE_GROUP4FAX);
if (i == 0) {
writer.write(null,img,param);
}
else {
writer.writeInsert(-1,param);
}
image.flush();
}
ios.flush();
ios.close();
writer.dispose();
}
catch(IOException ex) {
System.out.println("Error saving file " + ex);
ex.printStackTrace();
}
// clean up resources
document.dispose();
System.out.println("Done");
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)