Results 1 to 5 of 5
  1. #1
    Rob_ is offline Member
    Join Date
    Nov 2008
    Posts
    3
    Rep Power
    0

    Default Image Resizing and DPi

    Hi,

    Really struggling with a problem, I found a script that alters the dpi of an image, but I also need it to resize the image height/width. I've added the line;
    Java Code:
    BufferedImage thumb =  image.getScaledInstance(size,-1, BufferedImage.SCALE_SMOOTH);
    to do the resizing but it keeps coming up with the error;

    incompatible types
    found : java.awt.Image
    required: java.awt.image.BufferedImage
    BufferedImage thumb = image.getScaledInstance(size,-1, BufferedImage.SCALE_SMOOTH);
    ^
    1 error


    Any help would be really appreciated. I've add the script below.

    Many Thanks in advance

    Rob



    Java Code:
    import java.awt.Graphics;
    import java.awt.image.BufferedImage;
    import java.awt.Image;
    
    import java.io.File;
    import java.io.FileOutputStream;
     
    import javax.imageio.ImageIO;
    import javax.imageio.ImageReader;
    import javax.imageio.metadata.IIOMetadata;
    import javax.imageio.stream.FileImageInputStream;
    import javax.swing.ImageIcon;
     
    import org.w3c.dom.Element;
    import org.w3c.dom.Node;
     
    import com.sun.image.codec.jpeg.JPEGCodec;
    import com.sun.image.codec.jpeg.JPEGEncodeParam;
    import com.sun.image.codec.jpeg.JPEGImageEncoder;
     
    public class JpegDensity {
    	private ImageIcon image;
        private ImageIcon thumb;
    
    	public JpegDensity(Image image)
        {
            this.image = new ImageIcon(image); 
        }
        
        public JpegDensity(String fileName)
        {
            image = new ImageIcon(fileName);
        }
        
    	public static final int size = 600;	
        
    	public static void main(String[] args) throws Exception {
            File infile = new File("/fmimages/49433.jpg");
            File outfile = new File("/fmimages/density666.jpg");
    
            ImageReader reader = ImageIO.getImageReadersByFormatName("jpeg").next();
            reader.setInput(new FileImageInputStream(infile), true, false);
            IIOMetadata data = reader.getImageMetadata(0);
            BufferedImage image = reader.read(0);
    		BufferedImage thumb =  image.getScaledInstance(size,-1, BufferedImage.SCALE_SMOOTH);
    		
    		Element tree = (Element) data.getAsTree("javax_imageio_jpeg_image_1.0");
            Element jfif = (Element) tree.getElementsByTagName("app0JFIF").item(0);
            for (int i = 0; i < jfif.getAttributes().getLength(); i++) {
                Node attribute = jfif.getAttributes().item(i);
                System.out.println(attribute.getNodeName() + "="
                        + attribute.getNodeValue());
            }
     		FileOutputStream fos = new FileOutputStream(outfile);
            JPEGImageEncoder jpegEncoder = JPEGCodec.createJPEGEncoder(fos);
            JPEGEncodeParam jpegEncodeParam = jpegEncoder.getDefaultJPEGEncodeParam(thumb);
            jpegEncodeParam.setDensityUnit(JPEGEncodeParam.DENSITY_UNIT_DOTS_INCH);
            jpegEncodeParam.setXDensity(300);
            jpegEncodeParam.setYDensity(300);
            jpegEncoder.encode(thumb, jpegEncodeParam);
    		fos.close();
        	}
    }

  2. #2
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    10

    Arrow casting

    Quote Originally Posted by Rob_ View Post
    ....struggling....
    getScaledInstance(size,-1, BufferedImage.SCALE_SMOOTH); returns type Image - probably simple cast:
    Java Code:
    BufferedImage thumb = (BufferedImage) image.getScaledInstance(size,-1, BufferedImage.SCALE_SMOOTH);
    Note, this has some relation to film production but is not the same as Central Casting.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  3. #3
    Rob_ is offline Member
    Join Date
    Nov 2008
    Posts
    3
    Rep Power
    0

    Default

    Thanks a lot for your reply, it fixed the error I was having, but now it's producing another. Shown below

    Exception in thread "main" java.lang.ClassCastException: apple.awt.OSXImage
    at JpegDensity.main(JpegDensity.java:44)
    Again thanks in advance

    Rob

  4. #4
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    10

    Question what is OSXImage?

    What is OSXImage? Wrap all the code in try{}catch(Throwable t)System.out.println(t .getMessage()); and tell us what the exact error message it give is. Post it verbatim using copy-paste buffer.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  5. #5
    Rob_ is offline Member
    Join Date
    Nov 2008
    Posts
    3
    Rep Power
    0

    Default

    Hi Nicholas

    Thanks for your reply, I manage to solve the problem. Incase anyone is interested: (here's the working if not a little messy code)

    Java Code:
    import java.awt.*;
    import java.awt.image.*;
    import java.io.*;
    import java.net.*;
    import javax.imageio.*;
    
    import java.awt.Graphics;
    import java.awt.image.BufferedImage;
    import java.awt.Image;
    
    import java.io.File;
    import java.io.FileOutputStream;
     
    import javax.imageio.ImageIO;
    import javax.imageio.ImageReader;
    import javax.imageio.metadata.IIOMetadata;
    import javax.imageio.stream.FileImageInputStream;
    import javax.swing.ImageIcon;
     
    import org.w3c.dom.Element;
    import org.w3c.dom.Node;
     
    import com.sun.image.codec.jpeg.JPEGCodec;
    import com.sun.image.codec.jpeg.JPEGEncodeParam;
    import com.sun.image.codec.jpeg.JPEGImageEncoder;
     
    public class ImageManipulation {
            
    	public static void main(String[] args) throws Exception {
            File infile = new File("/35670v.jpg");
            File outfile = new File("/35670_600.jpg");
    
            ImageReader reader = ImageIO.getImageReadersByFormatName("jpeg").next();
            reader.setInput(new FileImageInputStream(infile), true, false);
            IIOMetadata data = reader.getImageMetadata(0);
    		BufferedImage image = reader.read(0);
    		
    		int w = 600, h = -1;
    	     Image rescaled = image.getScaledInstance(w, h, Image.SCALE_AREA_AVERAGING);
    	     BufferedImage output = toBufferedImage(rescaled, BufferedImage.TYPE_INT_RGB);
    		 
    		Element tree = (Element) data.getAsTree("javax_imageio_jpeg_image_1.0");
            Element jfif = (Element) tree.getElementsByTagName("app0JFIF").item(0);
            for (int i = 0; i < jfif.getAttributes().getLength(); i++) {
                Node attribute = jfif.getAttributes().item(i);
                System.out.println(attribute.getNodeName() + "="
                        + attribute.getNodeValue());
            }
     		FileOutputStream fos = new FileOutputStream(outfile);
            JPEGImageEncoder jpegEncoder = JPEGCodec.createJPEGEncoder(fos);
            JPEGEncodeParam jpegEncodeParam = jpegEncoder.getDefaultJPEGEncodeParam(output);
            jpegEncodeParam.setDensityUnit(JPEGEncodeParam.DENSITY_UNIT_DOTS_INCH);
            jpegEncodeParam.setXDensity(300);
            jpegEncodeParam.setYDensity(300);
            jpegEncoder.encode(output, jpegEncodeParam);
    		fos.close();
        	}
    
    	  public static BufferedImage toBufferedImage(Image image, int type) {
    	        int w = image.getWidth(null);
    	        int h = image.getHeight(null);
    	        BufferedImage result = new BufferedImage(w, h, type);
    	        Graphics2D g = result.createGraphics();
    	        g.drawImage(image, 0, 0, null);
    	        g.dispose();
    	        return result;
    	    }
    }

    Thanks again

Similar Threads

  1. Replies: 4
    Last Post: 01-13-2011, 06:30 PM
  2. Canvas Image popups another image (SWT)
    By SpaceY in forum New To Java
    Replies: 2
    Last Post: 11-11-2008, 02:25 PM
  3. drawing an image to an offscreen image
    By hunterbdb in forum Java 2D
    Replies: 9
    Last Post: 10-30-2008, 07:17 PM
  4. Replies: 0
    Last Post: 01-08-2008, 06:29 PM
  5. Image resizing
    By alley in forum Java 2D
    Replies: 2
    Last Post: 11-13-2007, 11:10 AM

Posting Permissions

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