失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > java将PDF转为tif文件_java - 使用JAI在JAVA中将二进制文本转换为Tiff图像/ PDF - 堆栈内存溢出...

java将PDF转为tif文件_java - 使用JAI在JAVA中将二进制文本转换为Tiff图像/ PDF - 堆栈内存溢出...

时间:2020-10-20 15:14:57

相关推荐

java将PDF转为tif文件_java - 使用JAI在JAVA中将二进制文本转换为Tiff图像/ PDF - 堆栈内存溢出...

我有不同的要求。 我正在接收TIFF图像作为二进制文本。 我不知道是否可以调用该二进制文本。 文本包含非ASCII字符,如下所示

0ÎÀi7°®èý¯Â£ôîÀk1ü"»£ð‚£Ê£ðü»£ö¿

ŒGÓº?¬hÄr€kðŠîÂ

ŒG*Àkð

¸z «ÿ*ëÿ¢^˾6‚¢êZÒáÿì)eì"‚("¿ÿ€jPšÄ0?P€ªê¨Eý5?J†¤=oöÃ|(0Ã6ª™P†!*¯Ä0ÿ*¢uÝ¡0Š­jþ &&—ÿ

+§¾È°Ã¡-s§‚2“³˜©Î{é¾pªXp%&ì;PËæ™4ºfŒ˜Îÿ Éû½)¨ŽV“þp¦IÇG˜bþñÿÿi•¼

因此,我很疲倦地使用以下代码使用imageIO读取此文本,但是会引发错误。

String str = "Binary Mentioned Above";

byte[] b = str.getBytes();

ByteArrayInputStream in = new ByteArrayInputStream(b);

BufferedImage bImageFromConvert = ImageIO.read(in);

TIFFEncodeParam params = new TIFFEncodeParam();

File myNewTIFF_File = new File("C:\\Projects\\test\\combined.tif");

ImageIO.write(bImageFromConvert, "TIFF", myNewTIFF_File);

我收到的错误消息是

Exception in thread "main" java.lang.IllegalArgumentException: image == null!

现在浏览这些帖子,我发现并不是所有的TIF都可以使用ImageIO读取。 因此,我在线使用了一个代码,该代码基本上将TIFF转换为PDF。

public static String ImageToPDF(byte[] bytes, String pathFile) {

String fileName= pathFile + ".pdf";

Document document = null;

document = new Document();

try {

FileOutputStream fos = new FileOutputStream(fileName);

PdfWriter writer = PdfWriter.getInstance(document, fos);

writer.open();

document.open();

// Array of bytes we have read from the Binary file

RandomAccessFileOrArray ra = new RandomAccessFileOrArray(bytes);

System.out.println("ra ---- "+ra);

// Get the number of pages the the binary file have inside

int numberOfPages = TiffImage.getNumberOfPages(ra);

System.out.println("numberOfPages ------------ "+numberOfPages);

// Loop through numberOfPages and add them on the document

// one by one

for(int page = 1; page <= numberOfPages; page ++){

Image image = TiffImage.getTiffImage(new RandomAccessFileOrArray(bytes),page);

image.scaleAbsolute(500, 500);

document.add(image);

}

document.close();

writer.close();

} catch (Exception e) {

e.printStackTrace();

}

return fileName;

}

public static void main(String[] args) throws IOException{

File imgFront = new File("C:\\Projects\\newtest.txt");

byte[] fileContent = Files.readAllBytes(imgFront.toPath());

//fileContent = File

ImageToPDF(fileContent,"C:\\Projects\\pdfWithImage");

}

我收到错误的错误的Bad endianness tag (not 0x4949 or 0x4d4d) 。 此错误来自此行TiffImage.getNumberOfPages(ra); 当我尝试阅读Tiff中的页面时。 我验证了使用Mirth工具创建tiff的二进制文本,并且tiff有效。 我没有足够的方法来解决此问题。 任何帮助深表感谢。

如果觉得《java将PDF转为tif文件_java - 使用JAI在JAVA中将二进制文本转换为Tiff图像/ PDF - 堆栈内存溢出...》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。