简体   繁体   English

在Itext中裁剪图像

[英]Cropping images in Itext

is there an easy way to crop an Image in Itext? 有没有一种简单的方法来在Itext中裁剪图像?

I have the following code: 我有以下代码:

URL url = new URL(imgUrl);

connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
iStream = connection.getInputStream();
img = new Jpeg(url);

// a method like 
// img.crop(x1, y1, x2, y2) would be nice.

Now I want to "delete" a strip of let's say 20 pixels left and 20 pixels right. 现在我想“删除”一条让我们说左边20像素,右边20像素。 Is there an easy way to do this? 是否有捷径可寻?

Here is another way to crop an image using PdfTemplate. 这是使用PdfTemplate裁剪图像的另一种方法。

public static Image cropImage(Image image, PdfWriter writer, float x, float y, float width, float height) throws DocumentException {
    PdfContentByte cb = writer.getDirectContent();
    PdfTemplate t = cb.createTemplate(width, height);
    float origWidth = image.getScaledWidth();
    float origHeight = image.getScaledHeight();
    t.addImage(image, origWidth, 0, 0, origHeight, -x, -y);
    return Image.getInstance(t);
}

Notice this doesn't require calling t.rectangle(), t.clip() or t.newPath(). 请注意,这不需要调用t.rectangle(),t.clip()或t.newPath()。

Nathan's solution almost worked for me. 内森的解决方案几乎对我有用。 The only problem left was white margins because of PdfTemplate having the same size as image to crop. 剩下的唯一问题是白边,因为PdfTemplate具有与要裁剪的图像相同的尺寸。

My solution: 我的解决方案

public Image cropImage(PdfWriter writer, Image image, float leftReduction, float rightReduction, float topReduction, float bottomReduction) throws DocumentException {
    float width = image.getScaledWidth();
    float height = image.getScaledHeight();
    PdfTemplate template = writer.getDirectContent().createTemplate(
            width - leftReduction - rightReduction,
            height - topReduction - bottomReduction);
    template.addImage(image,
            width, 0, 0,
            height, -leftReduction, -bottomReduction);
    return Image.getInstance(template);
}

You could investigate using the clipping path. 您可以使用剪切路径进行调查。 You'll need to know the width and height of the JPEG. 您需要知道JPEG的宽度和高度。 The code might look something like this: 代码可能如下所示:

PdfTemplate t = writer.getDirectContent().createTemplate(850, 600);
t.rectangle(x+20,y+20, width-40, height-40);
t.clip();
t.newPath();
t.addImage(img, width, 0, 0, height, x, y);

//4x6 inch photo height=432 width =288 // 4x6英寸照片高度= 432宽度= 288

// scale
if (isMatchFrame) {
    /* width */
    proportion = image.getWidth() / width;
    image.scaleAbsolute((float) width, image.getHeight() / proportion);
} else {
    /* hight */
    proportion = image.getHeight() / height;
    image.scaleAbsolute(image.getWidth() / proportion, (float) height);
}
// crop
document.setMargins((height - image.getHeight()/proportion) / 2, 0, (width - image.getWidth()/proportion) / 2 , 0);

can crop center photo. 可以裁剪中心照片。 enjoy it. 好好享受。

I also encountered this problem, and here's what i did: 我也遇到过这个问题,这就是我所做的:

Concept: Get the image as Buffered Image Make the BufferedImage and render as Image (iText) 概念:将图像作为缓冲图像生成BufferedImage并渲染为图像(iText)

Document document = new Document(PageSize.A4.rotate());
document.open();

//Get the image as Buffere Image
BufferedImage awtImage = ImageIO.read(new URL("image url"));

//Crop: Sample, get Upper Half of the image
BufferedImage awtImageUpper = awtImage.getSubimage(0, 0, awtImage.getWidth(), awtImage.getHeight()/2);

//Make BufferedImage and render as Image (in iText)
ByteArrayOutputStream baosImage = new ByteArrayOutputStream();
ImageIO.write(awtImageUpper, "png", baosImage);
Image iTextImage = Image.getInstance(baosImage.toByteArray());


//Display Image in pdf
document.add(new Paragraph("image Upper half"));
document.add((Element) iTextImage);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM