Can´t get absoluteX and absoluteY from image using itext - java

I try to get a image (jpg format) from url to put in my pdf using itext 5.0.5 with this simple code below:
Image imageToShow = null;
imageToShow = Image.getInstance(new URL("any image url here"));
imageToShow.scaleAbsolute(size[0], size[1]);
I get the image but the value from absoluteX and absoluteY is always a 'NaN' value and this problem prevents me to change this values (third line), what I'm doing wrong?

If you are in a situation where you can switch to another iText version, try out iText7.
The equivalent code would be:
// src url
String somePath = "https://www.pdfa.org/wp-content/uploads/2016/08/ITSC-Logo-Horizontal-RGB-300dpi.png";
// fetch image data
ImageData imageData = ImageDataFactory.create(new URL(somePath));
// turn into image object
Image image = new Image(imageData);
// perform scaling operation
image = image.scaleAbsolute(120f, 120f);
// debug
System.out.println(image.getImageWidth() + "x" + image.getImageHeight());

Related

Retrieve a BLOB image from database and display it as Image on javafx

I have an Image in a database as a Blob and I want to display it as an Image on javafx, I did this:
Image imagenMonstruo = null;
Blob blob = rs.getBlob(12);
InputStream is = blob.getBinaryStream();
System.out.println(is.toString());
imagenMonstruo = new Image(is);
is.close();
And added to the object but when I run the application i don't get any error but I still don't getting the image display, with the system.out.println I get:
java.io.ByteArrayInputStream#e0debc3
So I don't know if i'm doing this properly and the others similar question asked here doesn't work.
Can you help me please?
EDIT where i get the image from my pc and it works but my problem is the image from the database in Blob format.
File file = new File("C:\\\\xampp\\htdocs\\monsterHunter\\imagenes\\anjanath.jpg");
BufferedImage imagen = ImageIO.read(file);
Image imagenMonstruo = SwingFXUtils.toFXImage(imagen, null );

CMYK Image display on pdf using pdfbox

I am trying to create pdf in java using pdfbox 1.8, but problem is I am not able to show CMYK image on pdf so I try to solution on same like below code:
File filePath = new File("C:/Users/msuryawanshi/Documents/10734730431625_C1LA.jpg");
JPEGImageDecoder jpegDecoder = JPEGCodec.createJPEGDecoder(new FileInputStream(filePath));
BufferedImage image = jpegDecoder.decodeAsBufferedImage();
imageUrl = "http://extranet.handgards.com/gs1/10734730431625_C1LA.jpg";
File f = new File("C:/Users/msuryawanshi/Documents/10734730431625_C1LA.jpg");
String url = "http://extranet.handgards.com/gs1/10734730431625_C1LA.jpg";
Iterator readers = ImageIO.getImageReadersByFormatName("JPEG");
ImageReader reader = null;
while (readers.hasNext()) {
reader = (ImageReader) readers.next();
if (reader.canReadRaster()) {
break;
}
}
//Stream the image file (the original CMYK image)
ImageInputStream input = ImageIO.createImageInputStream(f);
reader.setInput(input);
//Read the image raster
Raster raster = reader.readRaster(0, null);
//Create a new RGB image
BufferedImage bi = new BufferedImage(raster.getWidth(), raster.getHeight(),
BufferedImage.TYPE_4BYTE_ABGR);
//Fill the new image with the old raster
bi.getRaster().setRect(raster);
PDXObjectImage ximage = new PDPixelMap(document, bi);
contentStream.drawXObject(ximage, margin + 5, texty, 170, 100);
but image is not meaningful, I have attached output pdf and original image which i want display on my pdf. please help for the same.
PDFBox doesn't support embedding CMYK images at all because java itself can't read such images. You might be able to embed it as an RGB image by using the twelvemonkeys library instead of Java ImageIO to read the JPEG into a BufferedImage. From there, just use PDPixelMap (in 1.8) or LosslessFactory (in 2.0).

Java ImageIO.read(URL) returning null when it shouldn't

Okay so I have spent about 15 hours trying to figure this out. I am working on this program that gets exported to a non-runnable jar file. The issue is I am trying to load an image in the jar and set it to a variable.
I HAVE looked at other posts and I think I have tried everything I could find but nothing seems to work.
I am not asking how to FIND the image as I can get the URL of the image, but then ImageIO.read(URL) is not throwing any exception, but returning null. The image is a .png which I have heard is compatible with ImageIO.read(). I am using an API so that is what the log() lines are.
Any help is appreciated. Thank you!
My Project:
Project
->src
-->package
--->Main.java
--->paint.png
My Code:
In my main method:
(mainPaint is a private Image)
mainPaint = getImage("paint.png");
The method:
private Image getImage(String fileName) {
URL url = getClass().getResource(fileName);
BufferedImage image = null;
log(url.toString()); // To make sure I have the correct file
// returning jar:file:/C:/Users/Me/MyJar.jar!/package/paint.png
try {
image = ImageIO.read(url);
} catch (IOException e1) {
log("Error converting url to image.");
// This is not happening
}
if (image == null) {
log("Image is null.");
// This is happening
}
return image;
}
Is the URL invalid? Am I just missing something? I'm just trying to save the local image in the jar as an Image object, I feel like this is way too difficult for what I am trying to do.
EDIT:
I also just tried making mainPaint a BufferedImage and using:
Image image = Toolkit.getDefaultToolkit().getImage(getClass().getResource(fileName));
if(image == null) {
log("Image is null");
}
log("Height: " + image.getHeight(null));
log("Width: " + image.getWidth(null));
BufferedImage bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
// Draw the image on to the buffered image
Graphics2D bGr = bimage.createGraphics();
bGr.drawImage(image, 0, 0, null);
bGr.dispose();
// Return the buffered image
return bimage;
The height and width of the image are returning -1?
ImageIO.read() will not load SVG files, so if your images are in SVG formate you will need to add plugins to it to support SVG
Here is another SO post that explain where you can do that.
ImageIO.read() will load only GIF, PNG, JPEG, BMP, and WBMP image types.
Any other image type will return null without error.
It could answer to your question .

codename one image from url

I am trying to make a mobile app using Java and codename one plugin. My question is - what's the simples way to populate an image from a URL to a label? I Googled it and all I found was this piece of code:
Image i = URLImage.createToStorage(placeholder, "fileNameInStorage", "http://xxx/myurl.jpg", URLImage.RESIZE_SCALE);
But I have no idea how to use it. What is a placeholder? It asks an EncodedImage parameter, but if I do:
EncodedImage image = new EncodedImage(10, 10);
I get the error that EncodedImage is protected.
I simply want to populate an image from a URL to my desired label in a form.
I am using GUI builder.
The placeholder image is the image that should show while the image from URL is being downloaded and it's an EncodedImage.
If your Label already have an icon as a placeholder, you can use its icon, otherwise you can create a new placeholder image. Below are 3 options to create an EncodedImage and a URLImage usage example:
Method 1:
//generate a grey placeholder that matches the size of the label's icon
Image placeholder = Image.createImage(label.getIcon().getWidth(), label.getIcon().getWidth(), 0xbfc9d2);
EncodedImage encImage = EncodedImage.createFromImage(placeholder, false);
Method 2:
//Convert the label icon to EncodedImage
EncodedImage encImage = (EncodedImage)label.getIcon();
Method 3:
//Create a fresh grey EncodedImage when label doesn't have any icon set initially
int deviceWidth = Display.getInstance().getDisplayWidth();
Image placeholder = Image.createImage(deviceWidth / 10, deviceWidth / 10, 0xbfc9d2); //square image set to 10% of screen width
EncodedImage encImage = EncodedImage.createFromImage(placeholder, false);
Usage Example:
It's a good practice to use the URL as the cached image name in storage. If you have a multiple sizes of the same image, just prefix them with a unique string like "Large" + URL
label.setIcon(URLImage.createToStorage(encImage, "Medium_" + "http://xxx/myurl.jpg", "http://xxx/myurl.jpg", URLImage.RESIZE_SCALE));

How to crop and rotate an image in pdf file in Java?

In Java, I have to crop and rotate an image in scanned pdf file and create another pdf file with the result image
For example, I have the image1 and want get the image2 centered in the page as result
How can I proceed?
Thanks!
The pdf is a scanned file.
It contains for example a student card.
I have to rotate the stutend card scanned on the right way and centered it in the A4 page.
I try to get the image like this :
PDDocument docIn = null;
File inputPdfFile = new File("C:\\test.pdf");
File output = new File("C:\\testOutput.png");
try {
// Read the pdf into a BufferedImage
docIn = PDDocument.load(inputPdfFile);
List<PDPage> pages = docIn.getDocumentCatalog().getAllPages();
PDPage page = pages.get(0);
// get the image
BufferedImage image = page.convertToImage(BufferedImage.TYPE_INT_RGB, 300);
ImageIO.write(image, "png", output);
} catch (IOException e) {
e.printStackTrace();
}
Then, I use the javaxt librairies to trim the image
Image image = new Image(output);
image.trim();
image.saveAs("C:\\projetCrop\\trimImage.png");
Now, how to detect the rotation angle?

Categories