itext 7.1如何检查图像是否旋转 [英] itext 7.1 how to check if image is rotated

查看:397
本文介绍了itext 7.1如何检查图像是否旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在itext 7.1中,我使用以下代码将图像添加到pdf文档中:

 文档文档=新文档( writerPdf); //创建一个新的文档对象
ImageData imgData = ImageDataFactory.create(imageBytes);
Image image = new Image(imgData);
document.add(image);

这适用于大多数图片,但我遇到的图片在桌面上看起来很正常但在添加时到pdf它旋转-90。

  imgData.getRotation()给出0作为输出

我的问题是:


  1. 如何检查图像是否有任何旋转设置。


  2. imgData.setRotation(90)似乎对我不起作用。如何旋转。
    谢谢。



解决方案

iText 7很遗憾一般不会读取(或至少不提供)该信息, ImageData 旋转属性当前仅针对TIFF文件提取。



如果图像具有EXIF元数据并且方向正确包含在其中,您可以尝试使用适当的库读取这些元数据并使用该方向插入使用iText的图片。



一个这样的图书馆是 Drew Noakes 元数据提取器,参见例如他的回答。它可以通过maven使用

 < dependency> 
< groupId> com.drewnoakes< / groupId>
< artifactId> metadata-extractor< / artifactId>
< version> 2.11.0< / version>
< / dependency>

有了这种依赖关系,你可以继续尝试:

 元数据元数据= ImageMetadataReader.readMetadata(new ByteArrayInputStream(imageBytes)); 
ExifIFD0Directory exifIFD0Directory = metadata.getFirstDirectoryOfType(ExifIFD0Directory.class);
int orientation = exifIFD0Directory.getInt(ExifIFD0Directory.TAG_ORIENTATION);

double angle = 0;
开关(方向)
{
案例1:
案例2:
angle = 0;打破;
案例3:
案例4:
angle = Math.PI;打破;
案例5:
案例6:
angle = - Math.PI / 2;打破;
案例7:
案例8:
angle = Math.PI / 2;打破;
}

凭证凭证=新凭证(writerPdf);
ImageData imgData = ImageDataFactory.create(imageBytes);
Image image = new Image(imgData);
image.setRotationAngle(angle);
document.add(image);

(来自 RecognizeRotatedImage test testOskar



(对于值2,4,5和7,实际上还需要翻转图像;对于更多背景看看例如这里 。)



为安全起见,请考虑将EXIF相关代码部分包装在适当的 try-catch 信封中。 / p>

in itext 7.1, I am adding an image to a pdf document with following code :

Document document = new Document(writerPdf);   //make a new document object
ImageData imgData = ImageDataFactory.create(imageBytes);
Image image = new Image(imgData);
document.add(image);

This works fine for most images but I have come across an image that seems normal on desktop but when adding to pdf it is rotated by -90 .

imgData.getRotation() gives 0 as output

My question is :

  1. how to check if image has any rotation set.

  2. imgData.setRotation(90) does not seem to work for me. How to rotate . Thanks.

解决方案

iText 7 unfortunately in general does not read (or at least not provide) that information, the Rotation property of ImageData currently is only extracted for TIFF files.

If the image has EXIF metadata and the orientation is properly contained in them, though, you can try and read those metadata using an appropriate library and use that orientation for inserting the image using iText.

One such library is Drew Noakes's metadata-extractor, cf. e.g. his answer here. It can be retrieved via maven using

<dependency>
    <groupId>com.drewnoakes</groupId>
    <artifactId>metadata-extractor</artifactId>
    <version>2.11.0</version>
</dependency>

With that dependency available, you can go ahead and try:

Metadata metadata = ImageMetadataReader.readMetadata(new ByteArrayInputStream(imageBytes));
ExifIFD0Directory exifIFD0Directory = metadata.getFirstDirectoryOfType(ExifIFD0Directory.class);
int orientation = exifIFD0Directory.getInt(ExifIFD0Directory.TAG_ORIENTATION);

double angle = 0;
switch (orientation)
{
case 1:
case 2:
    angle = 0; break;
case 3:
case 4:
    angle = Math.PI; break;
case 5:
case 6:
    angle = - Math.PI / 2; break;
case 7:
case 8:
    angle = Math.PI / 2; break;
}

Document document = new Document(writerPdf);
ImageData imgData = ImageDataFactory.create(imageBytes);
Image image = new Image(imgData);
image.setRotationAngle(angle);
document.add(image);

(from the RecognizeRotatedImage test testOskar)

(For values 2, 4, 5, and 7 one actually also needs to flip the image; for more backgrounds look e.g. here.)

To be safe, consider wrapping the EXIF related code parts in an appropriate try-catch envelope.

这篇关于itext 7.1如何检查图像是否旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆