eclipse plugin: save animated image into PC - java

I have a image loader which loads the image and save it into the PC as follows:
ImageLoader saver = new ImageLoader();
saver.data = new ImageData[] { ImageDescriptor.createFromURL(
FileLocator.find(bundle, new Path("icons/img.gif"), null))
.createImage().getImageData() };
saver.save("D:/img.gif", SWT.IMAGE_GIF);
But when i trying to save animated gif, the saved image is not animated. How could i save the animated image from the bundle to the user PC ?

As the code show, you have only one ImageData.
For an animated GIF, you need several ImageData. ImageDescriptor doesn't allow that; it is too high level, you need to use SWT directly:
final ImageLoader loader = new ImageLoader();
loader.load(FileLocator.find(bundle, new Path("icons/img.gif"), null).openStream()); // closing the stream would be appreciable :)
You can then try to save directly, but I think the SWT library is unable to save animated GIF.If you still want to use SWT for that, you must save each image one by one:
int i=0;
for (ImageData data : loader.data) {
final ImageLoader saver = new ImageLoader();
saver.save("image-" + (i++) + ".gif", SWT.IMAGE_GIF);
}

Related

Saving an AnimationDrawable object to a file

I am very new to android development and trying to make an application that will convert a video to GIf using MediaMetadataRetriever and AnimationDrawable. I am able to display the GIF but I am not able to save the file. I want to know how I can save this GIF as a file or essentially an AnimationDrawable object to a file in JAVA.
Thanks
MediaMetadataRetriever mmRetriever = new MediaMetadataRetriever();
mmRetriever.setDataSource(MainActivity.this,result);
AnimationDrawable animatedGIF = new AnimationDrawable();
Bitmap bitmap = mmRetriever.getFrameAtTime(n); #n is any integer
Drawable d = (Drawable) new BitmapDrawable(getResources(), bitmap);
animatedGIF.addFrame(d,200);
binding.imageView.setImageDrawable(animatedGIF);
animatedGIF.setOneShot(false);
animatedGIF.start();

How to make Java URI class to stop using a file

I have a small Java desktop application, it reads some image files and displays them. The problem is when I want to execute something (let me say, an exiftool operation) on these files, it denies because Java is still using them.
edit: This happens only on Windows, you can't write on an animated GIF file (converted to a URL object) which is being processed (being displayed) by Java, but on Ubuntu you can edit that file Metadata, system does not think the file is being processed.
Here is that part of my code.
I read the file;
ImageInputStream iis = null;
ImageReader reader = null;
iis = ImageIO.createImageInputStream(
f);
Iterator<ImageReader> imageReaders = ImageIO.getImageReaders(iis);
reader = (ImageReader) imageReaders.next();
Path pathe = f.toPath();
String mimeType = Files.probeContentType(pathe);
ImageIcon icon = null;
Here is the part I process images, resize them and make JLabel to view them.
The reason why I am not just using Java ImageIO to fill that label with an image, ImageIO can only display the first frame of an animated GIF file. Converting an image to URL this way keep the image animated (even after resized).
ImageIcon icon = null;
Integer labelWidth = this.imageLabel.getWidth();
Integer labelHeight = this.imageLabel.getHeight();
URI img;
img = f.toURI();
URL umg = img.toURL();
icon = new ImageIcon(umg);
//some calculations for setting labelWidth and labelHeight here
icon.setImage(icon.getImage().getScaledInstance(labelWidth, labelHeight,
Image.SCALE_DEFAULT));
At the end, streams are closed.
this.imageLabel.setIcon(icon);
iis.close();
reader.dispose();
And then I try to execute some exiftool commands via a process, it succesfully reads the image Metadata. But when updating the data, it says "Error renaming temporary file to C:/Users/path..." if the image is an animated GIF.
No problems occur if the image is a non animated image, JPEG, PNG or GIF, it can read and update the Metadata, I guess .
When I cancel the image displaying part of the code, I can write on the image Metadata without errors. If I read a JPEG file and display it, still no problems. If I read an animated GIF and display it (animated, does it keep the file connection open?) no modifications on this file can be done, not in my program nor on cmd.exe while the debugging session is not closed. After I quit debugging, exiftool process on cmd.exe starts working normally.
Closing ImageInputStream or ImageReader did not help.
Is there a way to make Java process (if the file is animated, I use URI, URL classes) release the file after read operation? Do these classes I mentioned have methods for releasing, closing, shutting down, kill process etc. I need to read the animated images and display them animated and make update operations on them.
thanks for the comments, here is the problem and the solution.
First of all I removed the unnecessary parts from the code. ImageInputStream and ImageReader were used to check image validation and detect image format (had to use different operations to GIF files) which I do not need anymore.
I still need to use File->URI->URL convertion to display animated GIFs. This is my old code.
URI img = f.toURI();
URL umg = img.toURL();
icon = new ImageIcon(umg);
This code kept connection to file open and blocked other processes editing the image file. (Only animated GIF files, on Windows system)
Here is the new code:
//these 2 lines are same
URI img = f.toURI();
URL umg = img.toURL();
InputStream is = umg.openStream();
byte[] byteChunk = new byte[4096];
int n;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while ((n = is.read(byteChunk)) > 0)
{
baos.write(byteChunk, 0, n);
}
byte[] dd= baos.toByteArray();
icon = new ImageIcon(dd);
is.close();
With this approach URI(image) is read via stream and ImageIcon is created from image file's byte array not directly from URL, and after that operation InputStream is closed, so the block on that file is released. (If "is.close()" line is not executed, file is still being processed and blocked for other write operations)
Have you tried to load the image with Toolkit.createImage method?
Give this a go:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Main {
private static class DrawPanel extends JPanel {
private Image i = null;
public void loadImage(final File f) {
//Most important point: load image via Toolkit.
//I think it must support GIF, JPEG and PNG.
i = getToolkit().createImage(f.getAbsolutePath());
repaint();
}
#Override
protected void paintComponent(final Graphics g) {
super.paintComponent(g);
if (i != null) {
//g.drawImage(i, 0, 0, this); //Draw full scale image.
g.drawImage(i, 0, 0, getWidth(), getHeight(), this); //Draw scaled/streched image.
//Supplying 'this' in place of ImageObserver argument to the drawImage method is also very important!
}
}
}
public static void main(final String[] args) {
SwingUtilities.invokeLater(() -> {
final DrawPanel imagePanel = new DrawPanel();
imagePanel.setPreferredSize(new Dimension(500, 350));
final JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
fileChooser.setMultiSelectionEnabled(true);
final JButton load = new JButton("Load");
load.addActionListener(e -> {
if (fileChooser.showOpenDialog(imagePanel) == JFileChooser.APPROVE_OPTION)
imagePanel.loadImage(fileChooser.getSelectedFile());
});
final JPanel contents = new JPanel(new BorderLayout());
contents.add(imagePanel, BorderLayout.CENTER);
contents.add(load, BorderLayout.PAGE_END);
final JFrame frame = new JFrame("Images");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(contents);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
});
}
}
There are several createImage methods to choose from.
I think they support animated GIFs, and non-animated JPGs and PNGs. I tested only for a GIF, but I have also worked with this in the past for JPGs and PNGs.
I think the animated image is completely loaded into memory (all frames). So you shouldn't have problem modifying the image after reading it.
Get the Toolkit related to the Component which will display the Image. I think also the Toolkit.getDefaultToolkit should do the job under certain circumstances. Then use the drawImage method of the Graphics object to draw the image. There are several drawImage methods to choose from. You can you use for example the one that scales the image on the fly for you (by supplying new width and height along with the draw location). Important: make sure you supply the drawImage with the component which renders it as the ImageObserver (note Component implements ImageObserver).

Android share text as image to other apps

I'm trying to share text from my app as an image to other apps. So I want a user to tap share on some content, have the app generate an image from the text, and add that to a chooser intent. (For example, sharing text from Twitter as an image on Instagram). I'm just not sure how to generate an image from the text and hand it in the proper format to the chooser. Any help is great, thanks!
One way is to create a Bitmap object from your TextView, storing it on disk and then sharing that file. This is how you can capture a View as a Bitmap object (courtesy of this answer):
public static Bitmap loadBitmapFromView(View v) {
Bitmap b = Bitmap.createBitmap( v.getLayoutParams().width, v.getLayoutParams().height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.layout(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
v.draw(c);
return b;
}
The rest should be easy enough.

Save SWT Image to File with Transparency Mask

This is my code thus far
ImageLoader saver = new ImageLoader();
saver.data = new ImageData[]
{ toSave.getImageData() };
saver.save(fileName, SWT.IMAGE_PNG);
toSave is an image that was loaded using the SWTResourceManager that is transparent in the program. fileName is a string representing the file I want to save the image to (ends with .png)
The result is an image with a black background instead of a transparent background that I want. How do I make the background transparent? I think it has something to do with the transparency mask, but I could be wrong.
Thanks in advance!
SWTResourceManager seems to be causing your problem. I would not recommend to use it.
Try this code, it works for me:
Display d = Display.getDefault();
Image image = new Image(d, "/pictures/Llama.gif");
ImageLoader saver = new ImageLoader();
saver.data = new ImageData[] { image.getImageData() };
saver.save("output.png", SWT.IMAGE_PNG);
image.dispose();
Remember to dispose the Image when you don't need it anymore.

How do I convert a RelativeLayout with an Imageview and TextView to a PNG image?

In my Android App Activity, I have a RelativeLayout with one ImageView and a couple of TextViews being populated at runtime.
I also have a Save button in the activity that I use to save the image in the ImageView to the device SD Card.
Now what I really want to do is Convert the elements (image and the text in the RelativeLayout) together to a PNG image when the Save button is clicked and Save it to the SD Card.
Have anyone tried a conversion like this before? It would be very helpful if someone can give me some hints or code snippets on how to go about doing this?
The Save functionality works fine but currently only saves the image in the imageview.
Thanks in advance.
RelativeLayout is a subclass of View, and the following should work for any view:
final View v; // The view that you want to save as an image
Bitmap bitmap = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bitmap);
v.draw(c);
File outputFile; // Where to save it
FileOutputStream out = new FileOutputStream(imageFile);
boolean success = bitmap.compress(CompressFormat.PNG, 100, out);
out.close();
Add exception handling at your leisure. ;)

Categories

Resources