I am going to do start a project soon and I will have to draw something on a .bmp/.jpg/.ps(any of these) in JFrame.
The pictures will present maps and I will have to generate some dots etc. on them how to do it in Java? Generally how to draw on a picture in Java?
Take a look at ImageIO api it has out of the box support for JPEG, PNG, BMP, WBMP & GIF. You can get TIFF support from the Advanced Image API.
Post script support is a little more tricky, but some PDF renderers can actually accomplish this (I used this approach to convert illustrator image formats).
There are some tutorials online. Take a look at Listing 14.17 on this site:
Java ist auch eine Insel
The code should be readable though the text is german. Hope it helps to make a first step into swing :)
About Bitmaps: I wrote my own Bitmap class to convert a png to a bmp, because the BitmapFactory and co didn't work for me. If you need it and nobody has a better "Java-API"-like solution, you can write me.
Related
I basically have an matrix of bytes. Each row (meaning byte[]) represents an image. How do I create a movie out of that (any format - avi, mpeg, whatever), and save it as a file?
Each image can be one of the following:
int JPEG Encoded formats.
int NV16 YCbCr format, used for video.
int NV21 YCrCb format used for images, which uses the NV21 encoding format.
int RGB_565 RGB format used for pictures encoded as RGB_565.
int YUY2 YCbCr format used for images, which uses YUYV (YUY2) encoding format.
int YV12 Android YUV format: This format is exposed to software decoders and applications.
I can choose the format to whatever I like, as long as I get to create the movie.
public void createMovie(byte[][] images) {
// and ideas on what to write here?
}
I don't need the actual implementation, just let me know the idea and what external libraries I need (if I need any).
I also need to edit some of the images (the byte stream) before I create the movie (to add some text). How can I do that?
The solution needs to be "Java only"! No external programs, no external commands (but I can use external jars).
Thanks!
The solution seems to be to use Mencoder (or at least, that seems to be a semi-popular choice).
Here's a link that specifically addresses images-to-movies capabilities in Mencoder.
As for rendering text onto the frames before encoding them as part of the video, you can use Java2D's image manipulation libraries to simply draw text on top of the images beforehand For example:
Load up the images into BufferedImage objects via the ImageIO library's .read method
Use Graphics2D's .drawString method to render the text
That's one way to do it, and this FAQ should get you started in that direction with Java2D, font rendering, etc., and offer pointers to further resources.
The ImageIO library also allows you to read/write a number of image formats, effectively allowing you to transcode images from, say, .jpg -> BufferedImage -> .png, or any which way you need to do it, if you want to store the image files temporarily during the conversion process, and/or convert all the images to a single format when importing them for the conversion project, etc.
Depending on how many output formats you want to support, you'll probably do something like
public void createMovie(BufferedImage[] frames, String destinationFormat)
...where "destinationFormat" is something like "m4v", "mpeg2", "h.264", "gif", etc.
Have you heard about JMF (Java Media Framework), from the sample you can find this example : Generating a Movie File from a List of (JPEG) Images
You can try making a gif with this gif encoder.
I wrote an MJPEG reader and writer for playing videos inside of Java applets. MJPEG is not the most advanced video format but it is very easy to manipulate. The code is part of my computer vision library BoofCV, but you could just rip out this one class for your own purposes.
Download this file: CreateMJpeg.java
Look at main function. Where it reads in jpeg images put your byte[] data, but you will need to convert it to jpeg's first.
You can convert it into a jpeg using the standard java library
Run modified code and enjoy your movie
Sorry its not in a more user friendly format, but at least you don't need to mess with JNI like some other solutions.
I have a play framework application which I want to be able to produce a product label from. I have the label design in illustrator. It consists of a black circle, white writing with a QR code in the middle, also has curved text.
I want to create a high resolution PDF and/or image file of this design on the fly. All most all of the drawing stuff I find for java relates to swing.
Anyone done this?
The basic class which allows creating an image programatically is BufferedImage and the corresponding Graphics2D class. You are not forced to use it with Swing. You can easily convert it to common graphic formats like PNG. Then you can save it as an image file or place it in a generated(e.g. with iText) PDF.
http://docs.oracle.com/javase/7/docs/api/java/awt/image/BufferedImage.html
http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics2D.html
In other words - yes, it can be done.
But if I ware you I would consider exporting the design from Illustrator to a file and use it as a resource in your application. But if you need to scale it programatically you ought to consider using SVG format to avoid loosing quality. Java does not have build-in support for vector images so you should look at
Apache Batik
I have attempted to find this answer (on Google and Stackoverflow) without success, but I'm sure it must have been asked before, so feel free to point me onwards to the answer if it exists.
Currently, I have a Java servlet that loads a PNG from disk into a BufferedImage, writes text on top of it, and then streams back the byte[] to the client.
My desire is to replace the PNG-from-disk with a rendered SVG path, from a collection of icon-paths that I've source online (e.g. "M21.871,9.814 15.684,16.001 21.871,22.188 18.335,25.725 8.612,16.001 18.335,6.276z" and "M22.727,18.242L4.792,27.208l8.966-8.966l-4.483-4.484l17.933-8.966l-8.966,8.966L22.727,18.242z").
I've come across Batik and SVG Salamander, but am struggling to understand how I would accomplish the above with either of them, most specifically, how to render the SVG path into the BufferedImage. I need to be able to specify (a) the dimensions of the image, (b) the fill-color and (c) & (d) the stroke width and color.
Here is an example that basically uses the Transcoder API.
I am converting pdf to tiff images for one of my project.
Than I using iipserver to generate tiles for the tiff images on fly. But this process is killing my CPU.
so I thinking of generating tile in advance and showing them directly instead of using iipserver. I researched into iipserver and got this libTiff c++ utility which is doing tiling work for the same server.
So I wanted to know is there any java wrapper for this libTiff or are there any other method from which i could generate tiles directly from tiff image or directly from pdf pages to tiles?
ImageJ can handle Tiled Pyramidal TIFF. JAI can handle MipMaps generated from TIFF files as well.
And if you're looking for a ready-made solution, take a look at djatoka.
In the end , I got the solution. There are few points that I want to explain
1) Tiles parameter in any tiff image is just a metadata value, so
there is nothing physically marking of tiles in an image
That best way I found to generate tiles out of image is BufferedImage class method:
bufferedImage.getSubimage(x, y, w, h)
Now play with this method in a loop for image matrix as per your needs.
It worked 200% perfect for me.. cheers to all :)
I basically have an matrix of bytes. Each row (meaning byte[]) represents an image. How do I create a movie out of that (any format - avi, mpeg, whatever), and save it as a file?
Each image can be one of the following:
int JPEG Encoded formats.
int NV16 YCbCr format, used for video.
int NV21 YCrCb format used for images, which uses the NV21 encoding format.
int RGB_565 RGB format used for pictures encoded as RGB_565.
int YUY2 YCbCr format used for images, which uses YUYV (YUY2) encoding format.
int YV12 Android YUV format: This format is exposed to software decoders and applications.
I can choose the format to whatever I like, as long as I get to create the movie.
public void createMovie(byte[][] images) {
// and ideas on what to write here?
}
I don't need the actual implementation, just let me know the idea and what external libraries I need (if I need any).
I also need to edit some of the images (the byte stream) before I create the movie (to add some text). How can I do that?
The solution needs to be "Java only"! No external programs, no external commands (but I can use external jars).
Thanks!
The solution seems to be to use Mencoder (or at least, that seems to be a semi-popular choice).
Here's a link that specifically addresses images-to-movies capabilities in Mencoder.
As for rendering text onto the frames before encoding them as part of the video, you can use Java2D's image manipulation libraries to simply draw text on top of the images beforehand For example:
Load up the images into BufferedImage objects via the ImageIO library's .read method
Use Graphics2D's .drawString method to render the text
That's one way to do it, and this FAQ should get you started in that direction with Java2D, font rendering, etc., and offer pointers to further resources.
The ImageIO library also allows you to read/write a number of image formats, effectively allowing you to transcode images from, say, .jpg -> BufferedImage -> .png, or any which way you need to do it, if you want to store the image files temporarily during the conversion process, and/or convert all the images to a single format when importing them for the conversion project, etc.
Depending on how many output formats you want to support, you'll probably do something like
public void createMovie(BufferedImage[] frames, String destinationFormat)
...where "destinationFormat" is something like "m4v", "mpeg2", "h.264", "gif", etc.
Have you heard about JMF (Java Media Framework), from the sample you can find this example : Generating a Movie File from a List of (JPEG) Images
You can try making a gif with this gif encoder.
I wrote an MJPEG reader and writer for playing videos inside of Java applets. MJPEG is not the most advanced video format but it is very easy to manipulate. The code is part of my computer vision library BoofCV, but you could just rip out this one class for your own purposes.
Download this file: CreateMJpeg.java
Look at main function. Where it reads in jpeg images put your byte[] data, but you will need to convert it to jpeg's first.
You can convert it into a jpeg using the standard java library
Run modified code and enjoy your movie
Sorry its not in a more user friendly format, but at least you don't need to mess with JNI like some other solutions.