I would like to create a gif image from the set of BufferedImages. How can I do this? Is there such library in pure Java (ImageMagick is not an option)? I've found Gif4J library but it's not royality-free.
I just answer a similar question here, but I think that my solution can help.
'ImageIcon' class allows you to load gif animations. I load the image with 'getResource()'. For doing this I normally us URL class to pass the file path. The path does not need to be necessary in a remote machine as the name URL may suggest.
URL url = This.class.getResource(path);
Icon myImgIcon = new ImageIcon(url);
JLabel imageLbl = new JLabel(myImgIcon);
component.add(imageLbl, BorderLayout.CENTER);
path will be the path of the gif inside of the class folder.
References:
http://docs.oracle.com/javase/tutorial/uiswing/components/icon.html#getresource
There is an image processing library, akin to Picasso which uses the very same AnimatedGifEncoder class mentioned by Lifelogger-
Glide Docs, Glide
AnimatedGifEncoder e = new AnimatedGifEncoder();
e.start(outputFileName);
e.setDelay(1000); // 1 frame per sec
e.addFrame(image1);
e.addFrame(image2);
e.finish();
Related
I use getResource() to load the image, but I get nullpointer exception. Also I tried to use css but I get nothing.
My code for label:
Image mine = new Image(getClass().getResourceAsStream("bomb3.png"));
ImageView im = new ImageView(mine);
Label label = new Label();
label.setGraphic(im);
And for button:
btn.setStyle("-fx-background-image: url('flag.png')");
This code is located in Painter class.
My file tree:
If you use getClass().getResourceAsStream("bomb3.png") java search in the packetof your class. So if your class is Game, your resource must be in the packet: com.name.minesweeperClasses
Image mine = new Image(getClass().getResourceAsStream("bomb3.png"));
here path of bomb3.png is incorrect
Please mention the relative path :-
./resources/boob3.png
I have save image path in database and image save in folder. Displaying image in list view app is stuck and slow. I not understand how to resolve this issue.
I am using following code:
strContactImage = customer.getPhotoPath();
Bitmap decodedImage = BitmapFactory.decodeFile(strContactImage);
customerImage.setImageBitmap(decodedImage);
customer.getPhotoPath() gets folder photo path image, and displays it properly, but when I added more than 20 records app slows down, and gets stuck.
Use uri to set image into image view instead of decoding into bitmap...
strContactImage = customer.getPhotoPath();
Uri uri=Uri.fromFile(new File(strContactImage));
// Set the uri into ImageView
customerImage.setImageURI(uri);
It might help you...
You can use any image caching library, here is the example of Picasso
strContactImage = customer.getPhotoPath();
Picasso.with(context)
.load(new File(strContactImage))
.into(customerImage);
check the size of the images they can effect the memory and make the app stuck and slow if they are take a lot of memory !
to fix that
take a look here
Load a Scaled Down Version into Memory
and check the method calculateInSampleSize() and decodeSampledBitmapFromResource()
it’s not worth loading a 1024x768 pixel image into memory if it will eventually be displayed in a 128x96 pixel thumbnail in an ImageView.
and you can use library that can help you to load image and they will take care about the memory usage and loading the image into Your View
Library :
Picasso
example :
Picasso.with(context).load(file).into(imageview);
Glide
example :
GlideApp.with(context).load(file).into(imageview);
Tools - check this for more about :
TinyPNG - Smart PNG and JPEG compression
Optimizilla
I cannot create an image in any of my JavaFX projects using the following kind of code:
final String url = "line.jpg";
Image image = new Image(Config.class.getResourceAsStream(url));
because there is always a null pointer exception pointing to the second line. Obviously, I have checked that the image file is in the correct directory. I have tried example programs, some directly copied from these boards, but these also fail for the same reason.
I suspect I lack a resource in Netbeans or JavaFX but I can't figure out what is missing.
The only workaround appears to be to include the image file in a css stylesheet and link it to the program by setting the gui components id like this:
Button homeButton = new Button();
homeButton.setId("homebutton");
In the stylesheet there is:
#homebutton {
-fx-background-image: url("images/homebtn.jpg");
-fx-pref-width: 30;
-fx-pref-height: 30;
}
Its not ideal being forced into this solution and Swing seems far better at dealing with image files. I assume a bug in JavaFX that always causes the following to fail:
Image image = new Image(Config.class.getResourceAsStream(url));
I have a problem, I can only create thumbnails of local video files but not of a remote url, here is my code:
bmThumbnail = ThumbnailUtils.extractThumbnail(ThumbnailUtils.createVideoThumbnail("http://download.blender.org/peach/bigbuckbunny_movies/BigBuckBunny_320x180.mp4", MediaStore.Video.Thumbnails.MINI_KIND), 50, 50);
I hope you can help me,
regards
christian
I suppose there is no easy way to build the thumbnail without actually downloading the video locally.
So if your question is 'Can I get a thumbnail without having to download the full video?', I'd say...no.
Otherwise, once you have downloaded the video locally, then I guess you can perfectly use ThumbnailUtils.createVideoThumbnail(...) by giving the path to the downloaded file.
I also have the same problem - but what can I say from my tests:
The problem occurs only on android >2.3
in android 2.0 -> 2.3 You can use just
Bitmap thumbnail = ThumbnailUtils.createVideoThumbnail( videoUrl, MediaStore.Video.Thumbnails.MINI_KIND);
I hope someone explain what change is on android 4. it doesn't work
I have no problem generating thumbnails from remote videos with the following code:
final Bitmap thumbnail = ThumbnailUtils.createVideoThumbnail( videoUrl, MediaStore.Video.Thumbnails.MINI_KIND );
You don't have to wrap an extractThumbnail() call around it
In my project I have 2 packages.
images - contain images and
notification - contain java files
In notification/main.java I get Image object from image using this code
Image image = Toolkit.getDefaultToolkit().getImage("/images/key-16x16.png");
and I can't get image.
How can I fix this bug.
I'm using Netbeans to develop Java desktop application and I have solved my problem.
Image image = new ImageIcon(this.getClass().getResource("/images/bell-icon16.png")).getImage();
"this" is a class extends JFrame
/ means an absolute path, save Java web-apps where / means relative to context. So, I would suggest to use relative URL, means get rid of that / in front, and then provide the right path.
In case, even then you can't solve it, try to create a file on the same path you are looking for image. This way you will know that where you are looking exactly and where you should look.
You could also try
Image image = new ImageIcon(path).getImage();
Incase the solution above doesn't work, try this (which worked for me):
Image image = Toolkit.getDefaultToolkit().createImage(this.getClass().getResource("/Images/bell-icon16.png"));
i've also been on this and it turns out that you need to create a package inside of your src folder.
for instace if you create a package called images inside of the src folder, your relative path will be
/images/yourimage.png.
Notice that the slash(/) must be there!
more info here http://forums.macrumors.com/showthread.php?t=533922
it worked for me
Toolkit tk = Toolkit.getDefaultToolkit();
Class<? extends JFrame> j = YOURJFRAME.getClass();
Image image = tk.createImage(j.getResource("/images/bell-icon16.png"));
Try that code, if you have a JFrame that will work.
If you have an Applet, then just use
Toolkit tk = Toolkit.getDefaultToolkit();
Image image = tk.createImage("images/bell-icon16.png");
With applets you never want to use the / in the beginning, but if you have a JFrame, and you are using getResource, you need the / in the beginning of the Path.
SwingResourceManager.getImage(YourClass.class,"key-16x16.png");
The getIcon method will return Icon as similar
setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("../resources/MainIcon.png")));