Page 1 of 2 12 LastLast
Results 1 to 20 of 34
  1. #1
    Cbani is offline Member
    Join Date
    Jan 2010
    Posts
    90
    Rep Power
    0

    Default Issue in reading .bmp file using javax.swing.ImageIcon

    Hi Guys,

    I am facing an issue while reading .bmp files using ImageIcon. Its working for jpeg and png.

    below is the code with a p.s.v.m(the main class) and follows the utility class which is helping in some read and write operation of the image files.

    the main class

    Java Code:
    import java.io.File;
    import java.net.FileNameMap;
    import java.net.URLConnection;
    
    public class thumbnailGenerator {
    
    	public static boolean generate(String source, String target, int width, int height, int thumbquality){
    		try{
    			
    			FileNameMap fileNameMap = URLConnection.getFileNameMap(); 
    			// Specifies the mime type
    			String mimeType = fileNameMap.getContentTypeFor(source); 
    			//String format = (mimeType.equalsIgnoreCase("image/png"))? thumbnailcreation.IMAGE_PNG : thumbnailcreation.IMAGE_JPEG;
    			String format = "";
    			if (mimeType!=null){
    				if (mimeType.equalsIgnoreCase("image/png")){
    					 format = thumbnailcreation.IMAGE_PNG;
    				} else if(mimeType.equalsIgnoreCase("image/jpeg")) {
    					format = thumbnailcreation.IMAGE_JPEG;
    				} else if(mimeType.equalsIgnoreCase("image/bmp")) {
    					format = thumbnailcreation.IMAGE_BMP;
    				} 
    			} else {System.out.println("default mime type");
    				format = thumbnailcreation.IMAGE_JPEG;
    			}
    			thumbnailcreation ct = new thumbnailcreation(source); 
    			// Fetches the thumbnail
    			ct.getThumbnail(width, thumbnailcreation.HORIZONTAL);
    			// Saves the thumbnail to the target location
    			ct.saveThumbnail(new File(target),source, format);  
    			
    		} catch (Exception e){
    				System.out.println("Thumbnail has not yet been created"); // Error
    		}
    		    return true;
    	}
    	public static void main(String[] args) {
    		
    		//generate(args[0],args[1],Integer.valueOf(args[2]).intValue(),Integer.valueOf(args[3]).intValue(),Integer.valueOf(args[4]).intValue());
    		generate("d:cbani////thumbnail_Src//105.img","d://cbani//thumbnail_target//105Copy.img",100,100,1);
    		
    	}
    }
    The utility class
    Java Code:
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.image.ColorModel;
    import java.awt.image.PixelGrabber;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    import javax.swing.ImageIcon;
    
    public class thumbnailcreation {
    
    	public static final int VERTICAL = 0;
    	public static final int HORIZONTAL = 1;
    
    	public static final String IMAGE_JPEG = "jpeg";
    	public static final String IMAGE_PNG = "png";
    	public static final String IMAGE_BMP = "bmp";
    
    	private ImageIcon image;
    	private ImageIcon imageIcon;
    	private ImageIcon thumb;
    
    	public thumbnailcreation(Image image) {
    		this.image = new ImageIcon(image);
    	}
    	
    	public thumbnailcreation(String fileName) {
    		image = new ImageIcon(fileName);
    	}
    
    	/*
    	 * This function is used to get the scaled thumbnail using image icon
    	 */
    
    	public Image getThumbnail(int size, int dir) {
    		if (dir == HORIZONTAL) {
    			thumb = new ImageIcon(image.getImage().getScaledInstance(size, -1,
    					Image.SCALE_SMOOTH));
    		} else {
    			thumb = new ImageIcon(image.getImage().getScaledInstance(-1, size,
    					Image.SCALE_SMOOTH));
    		}
    		return imageIcon.getImage();
    	}
    
    	/*
    	 * This function is used to get the scaled thumbnail using image icon
    	 */
    	public Image getThumbnail(int size, int dir, int scale) {
    		if (dir == HORIZONTAL) {
    			thumb = new ImageIcon(image.getImage().getScaledInstance(size, -1,
    					scale));
    		} else {
    			thumb = new ImageIcon(image.getImage().getScaledInstance(-1, size,
    					scale));
    		}
    		return thumb.getImage();
    	}
    
    	/*
    	 * This function is used to save the thumb nail to output directory
    	 */
    	public void saveThumbnail(File file, String source, String imageType) {
    		int BufferedImageType = 1;
    		if (thumb != null) {
    
    			if (imageType == "png") {
    				PixelGrabber pg = new PixelGrabber(thumb.getImage(), 0, 0, 1,
    						1, false);
    				try {
    					pg.grabPixels();
    				} catch (InterruptedException e) {
    				}
    
    				// Get the image's color model
    				ColorModel cm = pg.getColorModel();
    
    				BufferedImageType = (cm.hasAlpha() == false) ? BufferedImage.TYPE_INT_RGB
    						: BufferedImage.TYPE_INT_ARGB;
    			}
    			BufferedImage bi = new BufferedImage(thumb.getIconWidth(), thumb
    					.getIconHeight(), BufferedImageType);
    			Graphics g = bi.getGraphics();
    			g.drawImage(thumb.getImage(), 0, 0, null);
    			try {
    				ImageIO.write(bi, imageType, file);
    			} catch (IOException ioe) {
    				System.out.println("Error occured saving thumbnail");
    			}
    		} else {
    			System.out.println("Thumbnail has not yet been created");
    		}
    		(new File(source)).delete(); // delete temporary file before exiting
    		
    	}
    }
    first of all the mimetype for .bmp files is not coming as image/bmp
    secondly, thumb = new ImageIcon(image.getImage().getScaledInstance(size, -1,
    Image.SCALE_SMOOTH)); code is creating an image with width and hieght -1, -1. So, while trying to write the image by using saveThumbnail(), throwing exception.

    please review the code and suggest.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,901
    Rep Power
    25

    Default

    One of your problems with solving your problem is that you do NOT have a printStackTrace call in an exception block, so you are NOT seeing your errors.

  3. #3
    Maxideon is offline Member
    Join Date
    Jun 2010
    Posts
    28
    Rep Power
    0

    Default

    The Toolkit only understands GIF, JPEG, PNG, and a few other oddball formats. It doesn't read BMP images.

  4. #4
    Cbani is offline Member
    Join Date
    Jan 2010
    Posts
    90
    Rep Power
    0

    Default

    One of your problems with solving your problem is that you do NOT have a printStackTrace call in an exception block, so you are NOT seeing your errors.
    I had tried printing the stacktrace before posting; and its the same as i have mentioned.

    thumb = new ImageIcon(image.getImage().getScaledInstance(size, -1,
    Image.SCALE_SMOOTH)); code is creating an image with width and hieght -1, -1. So, while trying to write the image by using saveThumbnail(), throwing exception.
    It says can not write an image with width and height -1 -1.
    The reason as to why i hav gioven the complete code is because you guys could run it in your local environment and come back with some solutions.

  5. #5
    Cbani is offline Member
    Join Date
    Jan 2010
    Posts
    90
    Rep Power
    0

    Default

    The Toolkit only understands GIF, JPEG, PNG, and a few other oddball formats. It doesn't read BMP images.
    So, what changes would be required to make it read the .bmp files as well?

  6. #6
    Maxideon is offline Member
    Join Date
    Jun 2010
    Posts
    28
    Rep Power
    0

    Default

    Use ImageIO to do your writing and reading.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,778
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Maxideon View Post
    The Toolkit only understands GIF, JPEG, PNG, and a few other oddball formats. It doesn't read BMP images.
    In Java 1.6 when I do this:

    Java Code:
    System.out.println(Arrays.toString(ImageIO.getReaderFileSuffixes()));
    it's output is this:

    Java Code:
    [jpg, bmp, wbmp, jpeg, png, gif]
    So I guess it can do .bmp files ...

    kind regards,

    Jos

  8. #8
    Cbani is offline Member
    Join Date
    Jan 2010
    Posts
    90
    Rep Power
    0

    Default

    In Java 1.6 when I do this:

    Code:
    System.out.println(Arrays.toString(ImageIO.getRead erFileSuffixes()));
    it's output is this:

    Code:
    [jpg, bmp, wbmp, jpeg, png, gif]
    So I guess it can do .bmp files ...
    I am using Java1.6; but the same exception is thrown

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,778
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Cbani View Post
    I am using Java1.6; but the same exception is thrown
    What exception? My Java 1.6 version doesn't protest and ImageIO loads those .bmp files fine.

    kind regards,

    Jos

  10. #10
    Prajin's Avatar
    Prajin is offline Senior Member
    Join Date
    Jun 2010
    Location
    Ktm, Nepal
    Posts
    120
    Rep Power
    0

    Default

    The ImageIO does loads .bmp file, but I think OP is also using ImageIcon which don't support .bmp format and getting error.
    So if OP have error, show it here, then only we can tell what might me possible cause.
    ImageIcon support only(.gif, .jpg, & .png)

    -Regards

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    Strictly speaking, it's not a matter of ImageIcon supporting certain file types. ImageIcon reads the file contents using Toolkit, and it's Toolkit that doesn't support .bmp.

    db

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,901
    Rep Power
    25

    Default

    Without the printStackTrace call you do NOT see the NPE thrown because of this statement:
    Java Code:
    		return imageIcon.getImage();
    but the same exception is thrown
    What exception did you mention?

  13. #13
    Maxideon is offline Member
    Join Date
    Jun 2010
    Posts
    28
    Rep Power
    0

    Default

    Images returned by Image#getScaledInstance are Toolkit images and need to be loaded. When you switched to ImageIO for your reading purposes you probably got rid of all the ImageIcons. The ImageIcon class happens to load Toolkit images with its own MediaTracker.

  14. #14
    DMaharana is offline Member
    Join Date
    Aug 2010
    Posts
    14
    Rep Power
    0

    Default Is there any solution

    Is there any solution for it to resize the .bmp and use that.

    The requirement is I have to save the images as thumbnail layout. If I could not resize it then how can I use. means how can I able to see that big image as a thumbnail.

    So guys please find some solution for it...

  15. #15
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,901
    Rep Power
    25

    Default

    Can you post your current version of the program. The earlier version has a NPE in it.

  16. #16
    DMaharana is offline Member
    Join Date
    Aug 2010
    Posts
    14
    Rep Power
    0

    Default Issue in resizing the .bmp files

    Hi guys ,

    Still I am not getting any solution for resizing the .bmp file.

    The code above posted by Cbani is working for .jpg/.gif/.png images.
    To read the .bmp file I just added the code below in saveThumbnail method.

    if (imageType == "bmp") {
    try {
    bi = ImageIO.read(new File(source));
    } catch (IOException e) {
    e.printStackTrace();}

    It can read the bmp image and can able to save it in the target folder as mentioned in generate() method.
    But it can not resize the image. It just save the original sized image as it was in source folder. So I could not able to display it as a thumbnail.

    So guys please find some solution for it...


    Its very urgent....

  17. #17
    DMaharana is offline Member
    Join Date
    Aug 2010
    Posts
    14
    Rep Power
    0

    Default Please find the latest version of the code..

    Hi guys,

    Please find the above two java classes posted by me(DMaharana). and find some solution for it...

  18. #18
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,901
    Rep Power
    25

    Default

    Please go back and edit what you posted and add code tags.

    First thing I see is that you haven't followed our advice:
    Java Code:
    } catch (Exception e){
    System.out.println("Thumbnail has not yet been created"); // Error
    }
    Here you catch an exception and don't call: printStackTrace

  19. #19
    DMaharana is offline Member
    Join Date
    Aug 2010
    Posts
    14
    Rep Power
    0

    Default

    yes I hv added e.printStackTrace...
    As I am using VM so copy is restricted from there ...so I just added what I modified till now in the previous code.

  20. #20
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,901
    Rep Power
    25

    Default

    Please go back and edit what you posted and add code tags.
    If you've updated your code by adding calls to printStackTrace() post the new code.

Page 1 of 2 12 LastLast

Similar Threads

  1. javax.swing.RowSorter.SortKey
    By new2java2009 in forum New To Java
    Replies: 1
    Last Post: 04-11-2010, 04:26 AM
  2. download javax.swing.filechooser?
    By spalax in forum New To Java
    Replies: 5
    Last Post: 08-17-2009, 12:27 PM
  3. using javax.swing.JOptionPane in array
    By icedragon770 in forum AWT / Swing
    Replies: 12
    Last Post: 11-10-2008, 09:27 AM
  4. use of javax.swing.text.html
    By newbieal in forum New To Java
    Replies: 3
    Last Post: 10-09-2008, 07:45 PM
  5. map javax.swing.text.Element to javax.swing.text.View
    By elizabeth in forum New To Java
    Replies: 1
    Last Post: 07-30-2007, 08:02 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •