Display a Graphics2D as JPG in JSP - java

Is it possible to use Graphics2D in a servlet?
And if I have a Graphics2D object, is it possible for me to convert it to a .jpg image and display it in a JSP page?

In my opinion yes, Read the link for better understanding.

JFreeChart has a good example of doing this. To accommodate older versions, it provides a org.jfree.chart.encoders.SunJPEGEncoderAdapter. The encode() method shows how to use ImageIO for either byte array or stream.

Related

How can I know if the image is in RGB or BGR format?

Is there any way to know in advance if an image used as an input to a system is in RGB or BGR format?
I am using OpenCV with java API and I would like to convert an input image into grayscale or L*a*b* color space, but in OpenCV you have to specify first whether the image you want to convert is in RGB or BGR.
The type of the image I am using is either .jpg or .png.
If you are reading in the image file, or you have access to the code that reads in the file, know it is:
BGR order if you used cv2.imread()
RGB order if you used mpimg.imread() (assuming import matplotlib.image as mpimg)
If you don't know how the file was opened, the accepted answer BufferedImage is great for Java.
I would like to find a way to do the same in Python!
When you use opencv (imread, VideoCapture), the images are loaded in the BGR color space.
If your image is a BufferedImage then you can ask for his type with getType(), and test against the several constants (see: BufferedImage).

how to add text to image using imgscalr

I want to add some text to image. and I'm using imgscalr.
I'm looking for some examples or tutorials.
in case you know some better java based image library, please do let me know.
thanks!
Hmm, interesting suggestion. I never anticipated that people would want to render text to their images using imgscalr -- but with the popularity of meme images, maybe I should have thought of that :)
Since imgscalr is processing and returning standard BufferedImages in Java, you are free to use all the existing Java2D rendering libs to paint text to the image.
To do that you want to call BufferedImage.getGraphics() on the image you are processing, cast that to a Graphics2D object like so:
Graphics2D g2d = (Graphics2D)myImage.getGraphics();
Then you can use the standard Graphics2D font rendering methods to "draw" text onto your image. For example, something like:
g2d.drawString("I haz a hat!", 50, 50);
Also you can change how the font looks by using the Graphics.setFont(...) call.
In the future I may add convenience methods to imgscalr to do this for you now that you have given me this idea ;)

How to get a better understanding of a png representation on java?

I want to dive in the low level of how a png file is represented on memory in java, so that i can iterate over its pixels, change them, create a modified png file using existing one, etc.
Where do i begin?
You could begin by reading it into a BufferedImage with ImageIO.read(file) .
The getRGB(...) methods can help you to obtain information about the individual pixels, and the corresponding setRGB(...) methods help you to change them.
The representation of an image in memory in Java, is essentially unrelated to the format of the file: be it PNG, JPEG, GIF or whatever, those are standards for encoding an image as a (language independent) stream of bytes. But when you are manipulating the pixels of an image in memory, you have already decoded it, and so you've "forgotten" from which format (PNG, JPEG...) it came from.
The most common way of manipulating an image in Java is using the BufferedImage class, included in the java.awt.image.* package. But that's not a requisite. For instance, I've worked on a low level PNG coder/encoder (PNGJ) that does not use BufferedImage, but instead gives you each image line as an int[] array.

Java type that represents a PNG image?

What is a Java type which can hold a PNG implement and provide access to it's pixel buffer?
BufferedImage img = ImageIO.read(new File("my.png"));
int color = img.getRGB(23,12);
I would take a look at Java Advanced Imaging, it handle multiple types of image files.
Take a look ImageIO and its numerous static helpers for reading and writing bytes/streams containing an image.
If you want to do pixel based operations on the entire image, I've found calling the getRGB() method every time to be fairly slow. In that case, you might want to try and get access to the actual pixel array holding the image data using something like:
byte[] pixel_array = ((DataBufferByte)img.getRaster().getDataBuffer()).getData()
There may be a more flexible way that doesn't make any presumptions on the array data type.

Export JPanel to vector graphics

I would like to export the image in my JPanel to a vector graphics file so it can be edited and printed at a higher-than-screen resolution. Essentially I want its paint() function to be called with a destination Graphics that saves the drawing commands into a vector graphic file.
What is a good, simple way to do this? What libraries are recommended? Which vector format would be best, and why?
Have a look at The Java EPS Graphics2D package.
Many Java programs use Graphics2D to draw stuff on the screen, and while it is easy to save the output as a png or jpeg file, it is a little harder to export it as an EPS for including in a document or paper.
This package makes the whole process extremely easy, because you can use the EpsGraphics2D object as if it's a Graphics2D object. The only difference is that all of the implemented methods create EPS output, which means the diagrams you draw can be resized without leading to any of the jagged edges you may see when resizing pixel-based images, such as JEPG and PNG files.
Apache Batik will let you paint to a specialised implementation of a Graphics2D object and then export as an scalable vector graphics (.svg) file. You can then view/process/print it using an SVG-enabled browser (Firefox will handle it nativly, ISTR, IE and others can use plugins).
See the SVGGraphics2D object (process documented here)
The Java EPS mentioned by Pierre looks good, but if it isn't you might also like to look at FreeHEP Vector Graphics. Written to allow Java reuse in the High Energy Physics field it includes a vector graphics package, done through an implementation of Graphics2D. We've used it to export EPS very successfull for a number of years.
I can recommend the VectorGraphics2D library (LGPL). Although it does not support all the features of Graphics2D, I used it successfully for my project. It provides implementations of java.awt.Graphics2D for various vector file formats. It simply exports all paint operations to EPS, SVG, or PDF files.
Additional libraries for people with the same requirement:
for SVG, JFreeSVG at http://www.jfree.org/jfreesvg/
for PDF, OrsonPDF at http://object-refinery.com/orsonpdf/index.html
Both of these GPLv3 and well tested via extensive usage in JFreeChart and Orson Charts.
FreeHEP seems to work quite well although it does not appear to be maintained anymore and its bug and forum pages are gone. With just a handful of lines you get a popup dialog that can save any component to a variety of scalable and regular image formats. We have some challenging images, using alpha channel, rotated text, areas bounded by curves, and they saved perfectly, much better with with VectorGraphics2D.
The only problem I've seen so far is in jpeg save, which comes out black for all my images. This isn't very important for us given that png works, plus all the vector modes, but I'm sure it would be an issue for some.
I had to add exactly this much code to save in all these modes:
public static void showImage(Component comp)
{
try
{
ExportDialog export = new ExportDialog();
export.showExportDialog( null, "Export view as ...", comp, "export" );
System.err.println("Image save complete");
}
catch(Exception e)
{
e.printStackTrace();
}
}
There are a bunch of library jars that must be added as well.
this is basically not possible directly, as the low level java api works in terms of raster (pixels) and is never stored in vector format.
(Check the API of java.awt.Graphics to see what I mean).
there are some general purpose program that convert raster to vector formats, this is one I found on a quick search:
http://autotrace.sourceforge.net/index.html
so, using such a program you can divide your problem into two smaller problems:
convert your JPanel into a bitmap or file (http://www.jguru.com/faq/view.jsp?EID=242020)
run autotrace on the file.

Categories