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.
Related
I would like to read an image and then change its format but without saving it.
For example I can read the image like this
BufferedImage img=ImageIO.read(new File(fileName));
Then I want to change the format of img, for example from jpeg to png.
The only way I found is to use ImageIO.read to write and then read the new image, but it does seem to be an efficient way to do it.
When you "read" the image via
BufferedImage img=ImageIO.read(new File('myimage.png'));
you are not only reading but also decoding it, i.e., transforming the raw bytes in the (say) PNG format to some "RAW" format that your aplication or API can manipulate (or display) - in this case, a BufferedImage. Once this is done, the fact that this image came from a PNG file is forgotten. To read it as PNG and save it as JPEG you need to decode it (as PNG) and then code it (as JPG).
I would like to read an image and then change its format but without saving it.
The "format" of the image (in the PNG/JPEG sense) gives you a way of packing an image in a sequence of bits. So, your desire makes little sense. At most, you could store those bits in memory (what for?), but that would be the same as "saving it" (to memory instead of disk).
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.
What's the best Java library to use if I want to import graphics in some custom format (basically an array of bytes with a specified height, width, bytes per pixel and depth) and then work on it? Image resizing and cutting functionality would be useful if it was included. Should support saving to some lossless format too.
Ones I've found so far are too high level to import the raw data (or maybe it was hidden too deep...)
I've found SWT more than adequate. It allows images to be manipulated
at the pixel level. See this article and the Image Analyzer example.
For more take a look at SWT Home