Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Create image Jpeg from an object of Image class.

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    1
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Create image Jpeg from an object of Image class.

    I am working on a project on Steganography. I've succesfully encripted the source image with the text to be hidden, but i am unable to create an output image/encripted Jpeg from that object.
    How to create an image file (Jpeg) from an object of Image class? please reply asap.
     
    img2= createImage(new MemoryImageSource(iw,ih,pixels,0,iw));
    	     BufferedImage image = toBufferedImage(img2);
    	    save(image, "jpg");
     
     private static BufferedImage toBufferedImage(Image src) {
    	        int w = src.getWidth(null);
    	        int h = src.getHeight(null);
    	        int type = BufferedImage.TYPE_USHORT_565_RGB;  // other options
    	        BufferedImage dest = new BufferedImage(w, h, type);
    	        Graphics2D g2 = dest.createGraphics();
    	        g2.drawImage(src, 0, 0, null);
    	        g2.dispose();
    	        return dest;
    	    }
     
    	  private static void save(BufferedImage image, String ext) {
    	        String fileName = "savingAnImage";
    	        File file = new File(fileName + "." + ext);
    	        try {
    	            ImageIO.write(image, ext, file);  // ignore returned boolean
    	        } catch(IOException e) {
    	            System.out.println("Write error for " + file.getPath() +
    	                               ": " + e.getMessage());
    	        }
    	    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,355
    Thanks
    182
    Thanked 833 Times in 776 Posts
    Blog Entries
    5

    Default Re: Create image Jpeg from an object of Image class.

    In the code you posted it seems it has a method to save the Image...what about it does not work? Does it compile? Does it run? Does it throw exceptions? Does it behave?

  3. #3
    Forum VIP
    Join Date
    Jun 2011
    Posts
    378
    My Mood
    Busy
    Thanks
    49
    Thanked 111 Times in 94 Posts
    Blog Entries
    5

    Default Re: Create image Jpeg from an object of Image class.

    java.awt.Image is an abstract class. You use a BufferedImage or VolatileImage when you need an implementation. The use of Image in the toBufferedImage method means you wish to accept either a BufferedImage or VolatileImage and let the method resolve which one it is dealing with at run-time (polymorphism). My guess is that this is not what you want.

    Looking at your code it seems that toBufferedImage is actually drawing the image. Why not rename the function and remove that return BufferedImage. Instead, when you create the image, cast it directly into a BufferedImage like so:

    BufferedImage img2= (BufferedImage) createImage(new MemoryImageSource(iw,ih,pixels,0,iw));

    I wrote an Image processing tutorial not long ago. It has some useful code on how to open, save and manipulate images (it uses a TYPE_INT_ARGB so don't be surprised if the bitwise pixel operators don't work with your TYPE_USHORT_565_RGB). This example may also help you out.
    Last edited by ChristopherLowe; January 1st, 2012 at 12:08 AM. Reason: formatting

Similar Threads

  1. Convert image file to standard JPEG
    By AlexeiBlue in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: October 3rd, 2011, 09:00 AM
  2. Replies: 1
    Last Post: August 18th, 2011, 07:48 AM
  3. Replies: 2
    Last Post: February 14th, 2011, 05:36 PM
  4. How to create a new object of another class within a method
    By davie in forum Object Oriented Programming
    Replies: 1
    Last Post: April 16th, 2010, 06:53 PM
  5. Pixel Alteration in an image/image rotation
    By bguy in forum Java Theory & Questions
    Replies: 3
    Last Post: November 1st, 2009, 10:50 AM