Page 2 of 2 FirstFirst 12
Results 21 to 34 of 34
  1. #21
    Maxideon is offline Member
    Join Date
    Jun 2010
    Posts
    28
    Rep Power
    0

    Default

    Read an image.

    Java Code:
    BufferedImage img = ImageIO.read(new File(filePath));
    Scale the image

    Java Code:
    Image thumb = img.getScaledInstance(...);
    new ImageIcon(thumb); //forcibly load the thumbnail
    Save the scaled image.
    Java Code:
    BufferedImage thumbCopy = new BufferedImage(...);
    Graphics g = thumbCopy.createGraphics();
    g.drawImage(thumb,0,0,null);
    g.dispose();
    ImageIO.write(thumbCopy,new File(destination), format);
    Cleanup
    Java Code:
    thumbCopy.flush();
    thumb.flush();

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

    Default Issue Resolved...

    Thanks a lot Maxideon..! I can get now thumbnails for BMP images...

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

    Default

    ................................................

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

    Default

    thank u all guys for the solution :)

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

    Default This issue comes again...

    Hi guys,

    This code is working fine while unit testing, But when I integrate with my server then same issue back. It will not create thumbnail for bmp.

    Because this java code is called from an OScript code which is taking all images as .img format. so mimetype is always getting null. so it always comes to the else part of thumbnailGenerator.

    Please find the latest code--


    thumbnailCreator.java
    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;
    
    
    import org.apache.log4j.Logger;
    
    
    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";
    	public static final String IMAGE_GIF = "gif";
    
    	private ImageIcon image;
    	private ImageIcon thumb;
    	
    	private Logger logger = Logger.getLogger(getClass());
    
    	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 thumb-nail using image icon for Smooth Scaled instance 
    	 * @param size
    	 * @param dir
    	 * @return 
    	 */
    	public Image getThumbnail(int pSize, int pDir) {
    		logger.info("getThumbnail for jpg/gif/png is called:::1");
    		if (pDir == HORIZONTAL) {
    			thumb = new ImageIcon(image.getImage().getScaledInstance(pSize, -1, Image.SCALE_SMOOTH));
    		} else {
    			thumb = new ImageIcon(image.getImage().getScaledInstance(-1, pSize, Image.SCALE_SMOOTH));
    		}
    		return thumb.getImage();
    	}
    
    
    
    	/**
    	 * This function is used to get the scaled thumb-nail particularly for BMP Images.
    	 * @param size
    	 * @param dir
    	 * @param source
    	 * @return
    	 * @throws Exception
    	 */
    	public Image getThumbnail(int size, int dir, File source){
    		logger.info("getThumbnail for bmp is called");
    		try{
    			BufferedImage bi = ImageIO.read(source);
    			logger.info("Buffered Image:::"+bi);
    			Image temp;
    
    			if (dir == HORIZONTAL) {
    				logger.info("If part");
    				temp = bi.getScaledInstance(size, -1, Image.SCALE_SMOOTH);
    				logger.info("temp==="+temp);
    				thumb = new ImageIcon(temp);
    			} else {
    				logger.info("Else part");
    				temp = bi.getScaledInstance(-1, size, Image.SCALE_SMOOTH);
    				thumb = new ImageIcon(temp);
    			}
    		}catch(IOException e){
    			logger.error("Exception occured:::"+e);
    			System.out.println("Error Loading BMP Image");
    			e.printStackTrace();
    		}
    		return thumb.getImage();
    	}
    
    	/**
    	 * This function is used to save the thumb nail to output directory
    	 * @param file
    	 * @param source
    	 * @param imageType
    	 */
    	public void saveThumbnail(File file, String source, String imageType){
    		
    		logger.info("Now it is saving your thumbnail in the output folder");
    		int BufferedImageType = 1;
    		BufferedImage bi=null;
    	
    		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;
    			}
    			logger.info("1="+thumb.getIconWidth()+" 2="+thumb.getIconHeight()+" 3="+BufferedImageType);
    			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) {
    				ioe.printStackTrace();
    				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
    	}
    }

    thumbnailGenerator.java

    Java Code:
    import java.io.File;
    import java.net.FileNameMap;
    import java.net.URLConnection;
    
    import org.apache.log4j.Logger;
    
    public class thumbnailGenerator {
    	private static Class currentClass = thumbnailGenerator.class;
    	static Logger logger = Logger.getLogger(currentClass);
    	
    	/**
    	 * 
    	 * @param source
    	 * @param target
    	 * @param width
    	 * @param height
    	 * @param thumbquality
    	 * @return
    	 */
    	public static boolean generate(String source, String target, int width, int height, int thumbquality){
    		String openName = null;
    		try{
    			FileNameMap fileNameMap = URLConnection.getFileNameMap(); 
    			// Specifies the mime type
    			String mimeType = fileNameMap.getContentTypeFor(source); 
    			//String mimeType = new MimetypesFileTypeMap().getContentType(source);
    			//String format = (mimeType.equalsIgnoreCase("image/png"))? thumbnailcreation.IMAGE_PNG : thumbnailcreation.IMAGE_JPEG;
    			String format = "";
    			logger.warn("Input image type is:::"+mimeType);
    			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/gif")) {
    					format = thumbnailcreation.IMAGE_GIF;
    				} else if(mimeType.equalsIgnoreCase("image/bmp")) {
    					format = thumbnailcreation.IMAGE_BMP;
    				}
    				logger.info("Inside if format:::"+format);	
    			} else {
    				format = thumbnailcreation.IMAGE_JPEG;
    				logger.info("Inside else format:::"+format);
    			}
    
    			thumbnailcreation ct = new thumbnailcreation(source); 
    			// Fetches the thumbnail
    			if(format==thumbnailcreation.IMAGE_BMP){
    				logger.info("Input image is a bmp image");
    				ct.getThumbnail(width, thumbnailcreation.HORIZONTAL, new File(source));
    				//utils.loadbitmap("./", openName);
    			}else{
    				logger.info("Input image is a bmp renamed img image");
    				ct.getThumbnail(width, thumbnailcreation.HORIZONTAL);					
    			}
    			// Saves the thumbnail to the target location
    			ct.saveThumbnail(new File(target),source, "jpeg");  
    			
    		} catch (Exception e){
    			e.printStackTrace();
    			System.out.println("Thumbnail has not yet been created"); // Error
    		}
    		    return true;
    	}
    
    	public static void main(String[] args) {
    		logger.warn(new StringBuffer("Begin process: ").append(currentClass));
    		
    		//generate(args[0],args[1],Integer.valueOf(args[2]).intValue(),Integer.valueOf(args[3]).intValue(),Integer.valueOf(args[4]).intValue());
    		generate("E://thumbnail_Src//IMGWinter11.img","E://thumbnail_target//IMGWinter11.jpg",100,100,1);
    		
    	}
    }
    According to your solution I had added a new method specially for bmp images in thumbnailCreation.java. But as the oscript code is taking all images as .img image it is always coming to the else part of thumbnailGeneraor and getThumbnail() for bmp is called everytime. Hence now it is not working for png also.

    I think we are using ImageIO thats the problem.

    Please guys give a solution for it.

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

    Default

    What is the issue here?
    Is it that the variable mimetype is wrong?
    Or is it the code has problems writing .bmp files?

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

    Default

    No ...In the server the mimetype is getting null....Because it is taking all images as .img format.

    I think imageIO is not supporting here. Because locally while testing it is working f9.but not in server.
    And configuration is ok also.

    Is there any other option except ImageIO to create thumbnail for all mimetypes(.jpg/.png/.bmp/.gif).

    You can try the above code just to make a little change in thumbnailGenerator.

    In the else part
    Java Code:
    format = ThumbnailCreation.IMAGE_BMP;
    and test in your machine by creating two folders .it is working f9 with all mimetypes.

    But in the server it is not working...

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

    Default

    you just test here by changing the extension of the image to .img and you can know the problem according to the logs.

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

    Default

    Sorry, I don't have a server. Or any .img files for testing.

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

    Default

    I am not saying to test it in server. You can test this code locally and rename any image as imagename.img.

    I am saying the problem may be the server is not supporting ImageIO(what I guess).That's why it could not able to read the image.

    Is there any alternative for this ? Or can we convert one bmp to jpeg through code like this.

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

    Default

    The bmp image reader wasn't added to the standard jdk until java 1.5. You're server is probably running java 1.4. This isn't really a big deal, since you can get all the image reader and writer plug-ins made by sun here (including the bmp one). If you have java 1.3 or below, then you're out of luck.
    Last edited by Maxideon; 09-03-2010 at 03:05 PM.

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

    Default

    There is no mimetype for a file with the extension of .img on my system.
    Your code prints out: (I added source=" + source)

    Input image type is:::null source=images/Pic01.img

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

    Default

    Quote Originally Posted by DMaharana View Post
    To read the .bmp file I just added the code below in saveThumbnail method.
    ...
    Its very urgent....
    What's wrong with reading all images via ImageIO#read(...)?

    Recommended reading: How To Ask Questions The Smart Way

    db

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

    Default

    Please explain your problem and show your code. The original OPs problem was with using an image with the extension of .img and then asking the OS what mimetype the file was.
    The OS didn't examine the file to see what type of image file it was.

    Start a new thread for your new problem. Be sure to Say what version of java you are using.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. javax.swing.RowSorter.SortKey
    By new2java2009 in forum New To Java
    Replies: 1
    Last Post: 04-11-2010, 03:26 AM
  2. download javax.swing.filechooser?
    By spalax in forum New To Java
    Replies: 5
    Last Post: 08-17-2009, 11:27 AM
  3. using javax.swing.JOptionPane in array
    By icedragon770 in forum AWT / Swing
    Replies: 12
    Last Post: 11-10-2008, 08:27 AM
  4. use of javax.swing.text.html
    By newbieal in forum New To Java
    Replies: 3
    Last Post: 10-09-2008, 06: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, 07: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
  •