Put text on animated GIF and save it as a new GIF - java

I'm looking for a function that could process animated gif image to write text on top of it.
The working solution is probably provided by Gif4j lib, but I'm looking for open-source solution or advice how to implement it on my own.
How can I put text on a gif and save it as a new gif in Java?

You need to:
Split the animation into separate frames.
Write the text on each frame. For examples see answers like these (the first is more closely related, but the second source does the same, and is simpler).
Java Text on Image
How to resize text 1
Construct the frames into a new animated GIF. See Create animated GIF using imageio at the OTN, where I discuss the code used in a GIF animation tool.

Related

Android gif animation with ImageView

I have a .gif file with four frames, i used Ion library for animation but it doesn't show good result. Because this library shows previous frame along with animation. How can i fix that?
Here is a link to a relevant question Display Animated GIF that has already been answered. Essentially, it says that you can display .gifs with the android.graphics.Movie class. It has been available from API level 1. check out this code linked here https://code.google.com/p/apidemos/source/browse/trunk/ApiDemos/src/com/example/android/apis/graphics/BitmapDecode.java#103
and from line 103 down there is an implementation of it opening an gif resource

Create image from text with unknown number of lines

I would like to convert a string of text into an image. The issue is, I want the text to wrap if it is wider than the length of the image, and the height of the image to be dynamically sized to perfectly fit the text, so that I know how much space the text takes up.
I'm working in Java and there are several things I have tried:
Rendering HTML in a JPanel and saving as a BufferedImage. The problem here was that most of the css I used was ignored by the JPanel and the image was unusable.
Using ImageMagick and img4Java. The two big failures with this solution was that I needed the command-line tool installed, which I can't do on our server. The second was that I couldn't easily convert the image to buffered image for use in the rest of the app.
Does anyone know a way to do this in Java?
Thanks!
In this example, an arbitrary panel is rendered into a BufferedImage and displayed in an adjacent panel at half-scale. The example uses a grid of labels, but you can use the wrap feature of JTextArea or the geometry supplied by TextLayout, examined here.
You might use a label containing HTML for the line-wrap, as shown here.
To get an image of that, see LabelRenderTest.

JavaFX: get BufferedImage from path

Coming from AWT/Swing, I've started experimenting a bit with JavaFX the last few days. I realized that what I used to do in thousands lines of codes can now be done in a few hundred.
One problem I came across is, however, the following: I'm trying to develop a small painting app where the user can choose brush size and color for its strokes. For all the strokes the user makes, I use the JavaFX class Path and add these paths to a Group (which is added to a Pane) where they are - automagically - painted. Now I want to store the resulting image as a jpg and try to raster all the paths in a BufferedImage. However, I found no functions in the API that help me do that.
I tried to use Canvas and its GraphicsContext, but that did not help. How could I raster all the JavaFX Paths from a list on an image?
Take a snapshot of your Group to get a JavaFX Image.
Use SwingFXUtils to convert your JavaFX image snapshot to a buffered image.
Use ImageIO to convert your buffered image to a jpeg, png, etc

Java: get frames from animated GIF without ImageReader

I'm developing AppEngine application. One of it's features is splitting an animated .gif image into separate frames. I've searched a lot to find the way how to do it and finally found the solution. Unfortunately the solution is based on ImageReader and I cant use it on the server, because:
javax.imageio.ImageReader is not supported by Google App Engine's Java
runtime environment
Are there any other ways to decode GIF-image without this class?
First some thing about frame itself. There are two implications about splitting an animated .gif image into separate frames. 1) Literally, a frame is a frame in the sense of an animated GIF. The problem is frames which constitute an animated GIF image are related. The disposal method of an animated GIF dictates what to do with the previous frame when drawing the current frame. You can override it; fill with background color before drawing the new frame, or you can do whatever you think appropriate before drawing the new frame. If you think the above situation is complicated, what about transparency of frames? logical position to draw each frames?
If we go along this road, there is no need to use a dedicated ImageReader, just read relevant parts of the image and copy each frame data, save it along with a header and color palette. The consequence is: the resulting image might look weird and meaningless. Look at the example below:
The first frame
The second frame
And the original
You can see the second frame doesn't look so good. The truth is, the second frame is a transparent one which build on top of the first frame (this animated GIF only contains 2 frames). You are expect to see through the second frame and altogether, they make an animation.
Now let's see what the second implication of splitting an animated .gif image into separate frames. 2) In this case, the frame is a actually is a composite which builds upon the previous frames and which is what we are seeing when viewing an animated GIF. In order to achieve this, we have to take into effect the history of the frame loop, the logical position of each frame, and the transparency of the frames themselves.
Let's see what we get now:
The first frame
The second frame
Now the first frame is the same as in the first situation, but the second frame is constructed on top of the first one and it's not transparent anymore.
In the second case, we do have to decode and encode the frames to achieve the desired result. Besides looking nice, another good thing about this is you can save the resulting images in any format the encoder support.
The examples in this post are generated by the GIF related part of iCafe

How to merge two GIFs in Java?

I have to GIFs with the same size. One in animated and transparent, the other one is only plaintext. How can I merge both so that the animated one is on top and I can see the text tru the transparent parts and I only have to display one GIF in my app?!
Cheers
'Layers' is what first comes to mind and what seems to be the solution in this related question:
Image handling in Java

Categories