Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-21-2008, 05:17 PM
Member
 
Join Date: Nov 2008
Posts: 3
Rep Power: 0
Rob_ is on a distinguished road
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;
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



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();
    	}
}
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 11-23-2008, 04:26 PM
Nicholas Jordan's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Southwest
Posts: 1,018
Rep Power: 3
Nicholas Jordan is on a distinguished road
Arrow casting
Originally Posted by Rob_ View Post
....struggling....
getScaledInstance(size,-1, BufferedImage.SCALE_SMOOTH); returns type Image - probably simple cast:
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.
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-24-2008, 01:28 PM
Member
 
Join Date: Nov 2008
Posts: 3
Rep Power: 0
Rob_ is on a distinguished road
Default
Thanks a lot for your reply, it fixed the error I was having, but now it's producing another. Shown below

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

Rob
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-27-2008, 07:12 AM
Nicholas Jordan's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Southwest
Posts: 1,018
Rep Power: 3
Nicholas Jordan is on a distinguished road
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.
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 11-27-2008, 02:21 PM
Member
 
Join Date: Nov 2008
Posts: 3
Rep Power: 0
Rob_ is on a distinguished road
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)

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
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Flicker with resizing and need general code examination MrFreeweed Java 2D 2 11-17-2008 05:41 AM
Canvas Image popups another image (SWT) SpaceY New To Java 2 11-11-2008 01:25 PM
drawing an image to an offscreen image hunterbdb Java 2D 9 10-30-2008 06:17 PM
Converting multiple banded image into single banded image... Image enhancement archanajathan Advanced Java 0 01-08-2008 05:29 PM
Image resizing alley Java 2D 2 11-13-2007 10:10 AM


All times are GMT +2. The time now is 03:34 AM.



VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org