Load tif images in Vaadin - java

I could not load a tif image with Vaadin 7 rc1
Here are the some of the alternatoves I have tried:
String basepath = VaadinService.getCurrent().getBaseDirectory().getAbsolutePath();
Image image = new Image("Image", new FileResource(new File(basePath + "myPath/image.tif")));
Embedded image = new Embedded("Image", new ThemeResource("/pathToResource/imgae.jtif"));
In both cases the image was where the path pointed. Moreover if I had a jpg or png image in the same directory it was shown without a problem.
Does Vaadin 7 rc1 not support tif images?

The real question here is that if your browser supports TIFF images. Take a look at this wikipedia article.
For cross-browser compatibility, I suggest you to convert your images to PNG or JPG.

Related

Jpg with black background when extracted via PDFBox from a PDF file for images with define colorspaces, works fine for other images

Trying to get save embedded images in pdfs using Pdfbox 1.6.
Works fine for some images but doesn't work(creates full black image) for those that have defined colorspace.
Colorspace details : ICCBased , Number of Components : 3;
PDXObjectImage o = (PDXObjectImage)page.getResources().getImages().get("Im0");
Both
ImageIO.write( o.getRGBImage(), "png", file);
and o.write2file(file);
return in same result.
Please advise how to save these images correctly.

Why use ImageIO can't get BufferedImage from URL

imageURL: https://fbexternal-a.akamaihd.net/safe_image.php?d=AQBB77GLfY75FNWL&w=720&h=2048&url=http%3A%2F%2Fwww.facebook.com%2Fads%2Fimage%2F%3Fd%3DAQI0duFYFcmydWNutbwmSk2DfOmHcDrhPfsMJTUoEObbWkVzYUtrHgCuN_LFrWcPRzJi6jPgbn80oFs0Kj_WrdROjdnJkjbnS5-UJv9l9cJyhKCWS-lr-MXlc263Ul3Txe-VFqXfRrA6BOjt4DF-Sww2&ext=best
URL url = new URL(imageURL);
BufferedImage image = ImageIO.read(url);
or
URL url = new URL(imageURL);
BufferedImage image = ImageIO.read(url.openStream());
the result image is null? why?
ImageIO.read(URL) does support reading images from URL like you describe, however, it does support only a limited set of image formats. Built-in formats are JPEG, PNG, GIF, BMP and WBMP. There are plugins for many other formats, like TIFF, JPEG 2000, etc.
The problem is that the linked image is not in any of the built-in formats, it's in WEBP format, a new image format created by Google, and which does not have very widespread use yet. The reason it displays fine in your browser (and mine :-) ), is most likely that you are using Chrome, and Chrome has built-in support for WEBP.
There's at least one WEBP ImageIO plugin available. If you build and install this plugin, your code above should work and read the image just fine. There should be no need to invoke ImageIO.scanForPlugins() if the plugin is on class path when you launch your application.
Make sure the URL your provided is linking to a valid image file format such as jpg, png, bmp and so on.
Your current URL is linking to a .php file which is obviously not an image.
For example:
public static void main(String[] args)
{
Image image = null;
try {
URL url = new URL("https://s-media-cache-ak0.pinimg.com/236x/ac/bb/d4/acbbd49b22b8c556979418f6618a35fd.jpg");
image = ImageIO.read(url);
} catch (IOException e) {
e.printStackTrace();
}
JFrame frame = new JFrame();
frame.setSize(236, 306);
frame.add(new JLabel(new ImageIcon(image)));
frame.setVisible(true);
}
When I open the link in Firefox, it attempts to download a file called "safe_image.php". It works in Google Chrome, so there's something weird going on in the headers or something for that URL. Is there any way you can host the image elsewhere?
Edit: Your image appears to be in the WebP format. ImageIO does not support this format natively, so you will need a library like webp-imageio.
Install that library and see if your code works. ImageIO should automatically find the plugin when you run ImageIO.scanForPlugins().

Is there an efficient way to crop out PDF and save as image (.JPG) via java program?

I am Currently using ICEPDF to render PDF files and display it in my java Swing application (in Internal Frame). Now I want to add crop features to my Java application. Like, if I click a button, I can drag required portion of PDF and save it as Image in my local storage.
Is there an efficient way to crop out PDF and save as image (.JPG) via java program?
Ghost4J library (http://ghost4j.sourceforge.net), is your best option:
3 simple step:
Load PDF files:
PDFDocument document = new PDFDocument();
document.load(new File("test.pdf"));
Create the renderer:
SimpleRenderer renderer = new SimpleRenderer();
// set resolution (in DPI)
renderer.setResolution(600);
Render:
List<Image> images = renderer.render(document);
Then you can do what you want with your image objects, for example, you can write them as JPG like this:
for (int i = 0; i < images.size(); i++) {
ImageIO.write((RenderedImage) images.get(i), "jpg", new File((i + 1) + ".jpg"));
}
Ghost4J uses the native Ghostscript API so you need to have a Ghostscript installed.
EDIT: investigating a bit, if you convert the PDF to Image you won't have much problem to crop them:
BufferedImage is a(n) Image, so the implicit cast that you're doing in the second line is able to be compiled directly. If you knew an Image was really a BufferedImage, you would have to cast it explicitly like so:
Image image = ImageIO.read(new File(file));
BufferedImage buffered = (BufferedImage) image;
Then you can crop it with BufferedImage::getSubimage method:
private BufferedImage cropImage(BufferedImage src, Rectangle rect) {
BufferedImage dest = src.getSubimage(rect.x, rect.y, rect.width, rect.height);
return dest;
}

Unable to load image from the same package in Java?

In my java package, I have a file called 'prog.ico'. I'm trying to load this file, via the following code:
java.net.URL url = this.getClass().getResource("prog.ico");
java.awt.Image image = ImageIO.read( url );
System.out.println("image: " + image);
This gives the output:
image: null
What am I doing wrong? The .ico file exists in the same package as the class from which I'm running this code.
It seems that the .ico image format is not supported. See this question and it's answer to get around this.
To prevent link rot: This solution recommends using Image4J to process .ico files.
I've written a plugin for ImageIO that adds support for .ICO (MS Windows Icon) and .CUR (MS Windows Cursor) formats.
You can get it from GitHub here: https://github.com/haraldk/TwelveMonkeys/tree/master/imageio/imageio-ico
After you have it installed the plugin, you should be able to read your icon using the code in your original post.
I thing you must go over FileInputStream to wrap the file
File file = new File("prog.ico");
FileInputStream fis = new FileInputStream(file);
BufferedImage image = ImageIO.read(fis); //reading the image file

GWT Image setUrl()

i am using - and new to - GWT and i made an Image Widget to view an image. This image is found on my file system i wrote.
String src = "file:///D:/myfolder/myfile.jpg";
Image image = new Image();
image.setUrl(src);
but image doesn't appear!
You should specify either the whole URL (http://www.example.com/img/myfile.jpg) or (better yet) just relative to the root: /img/myfile.jpg. And, of course, you have to place your images in your WAR directory.
That's the simple setup. If you have more images and want to optimize fetching them (many images -> many requests to the server), have a look at the ClientBundle.

Categories

Resources