Results 1 to 1 of 1
  1. #1
    FrozrnNova is offline Member
    Join Date
    Aug 2013
    Posts
    1
    Rep Power
    0

    Default Image Editing & Saving

    I'm currently using Java to do some very basic image editing, essentially just doing some pixel replacement based on color distance, nothing big. The real issue I'm running into is trying to save the end result. It could be the fact that I'm using a premade Picture.java, that I had to grab for high school, in which I'm adding the code to, but I can't figure out how to save the image. Since any code we wrote previously to change an image was simply sent to the teacher so they could run it themselves, there was never a need to save the end result.

    Everything above the method prettyNoah was already placed within the Picture.java file, prettyNoah being the method use do the editing. This was done on a clean copy of Picture.java after attempting to follow several different ways of saving, but it always came down to something involving SimplePicture not being a bufferedImage, and my recent introduction into Java hasn't given me the ability to troubleshoot beyond that. I doubt I'd be able to recreate the errors and nullPointers that showed up.

    Most ways I saw to save involve using bufferedImage, but I can't wrap my head around that either. Any help would be much appreciated.




    Java Code:
    import java.awt.*;
    import java.awt.font.*;
    import java.awt.geom.*;
    import java.awt.image.BufferedImage;
    import java.text.*;
    import java.util.*;
    import java.io.*;
    import java.util.List; // resolves problem with java.awt.List and java.util.List
    
    /**
     * A class that represents a picture.  This class inherits from 
     * SimplePicture and allows the student to add functionality to
     * the Picture class.  
     * 
     * Copyright Georgia Institute of Technology 2004-2005
     * @author Barbara Ericson ericson@cc.gatech.edu
     */
    public class Picture extends SimplePicture
    {
    
      
    ///////////////////// constructors //////////////////////////////////
      
      /**
       * Constructor that takes no arguments 
       */
      public Picture ()
      {
        /* not needed but use it to show students the implicit call to super()
         * child constructors always call a parent constructor 
         */
        super();  
      }
      
      /**
       * Constructor that takes a file name and creates the picture 
       * @param fileName the name of the file to create the picture from
       */
      public Picture(String fileName)
      {
        // let the parent class handle this fileName
        super(fileName);
      }
      
      /**
       * Constructor that takes the width and height
       * @param width the width of the desired picture
       * @param height the height of the desired picture
       */
      public Picture(int width, int height)
      {
        // let the parent class handle this width and height
        super(width,height);
      }
      
      /**
       * Constructor that takes a picture and creates a 
       * copy of that picture
       */
      public Picture(Picture copyPicture)
      {
        // let the parent class do the copy
        super(copyPicture);
      }
      
      /**
       * Constructor that takes a buffered image
       * @param image the buffered image to use
       */
      public Picture(BufferedImage image)
      {
        super(image);
      }
      
      ////////////////////// methods ///////////////////////////////////////
      
      /**
       * Method to return a string with information about this picture.
       * @return a string with information about the picture such as fileName,
       * height and width.
       */
      public String toString()
      {
        String output = "Picture, filename " + getFileName() + 
          " height " + getHeight() 
          + " width " + getWidth();
        return output;
        
      }
      
      
      public static void main(String[] args) 
      {
         String fileName = FileChooser.pickAFile();
         Picture pictObj = new Picture(fileName);
         pictObj.explore();
      }
      
    
    
    // METHOD to swap BG - replaces current BG with a new BG ----  newBG is the replacement background
               public void prettyNoah(Picture newBG) 
               { 
                 // Set color to be replaced to white, defining pixels to be used as reference for color distance and color replacement
                 Color bg = new Color(255, 255, 255); 
                 Pixel noahPixel = null; Pixel bgPixel = null; 
                 
                 // Cycle through all x and y values of the two images, preferably of same dimensions
                 for(int x = 0; x < this.getWidth(); x++)
                 {
                   for(int y = 0; y < this.getHeight(); y++)
                   { 
                     // pixel from image selected ---- Picture p = new Picture(FileChooser.pickAFile())
                     noahPixel = this.getPixel(x,y); 
                     // pixel from image selected ---- Picture q = new Picture(FileChooser.pickAFile())
                     bgPixel = newBG.getPixel(x,y); 
                     // setting sensitivity of Color distance to determine pixel replacement
                     if(noahPixel.colorDistance(bg) > 15.0) 
                     { 
                       // setting color of noahPixel to bgPixel
                       noahPixel.setColor(bgPixel.getColor()); 
                     } 
                   }
                 }
               }
               
               
              
      
    } // this } is the end of class Picture, put all new methods before this
    Last edited by FrozrnNova; 08-15-2013 at 03:20 AM.

Similar Threads

  1. image editing problem
    By sambar89 in forum AWT / Swing
    Replies: 14
    Last Post: 12-19-2011, 04:05 PM
  2. Need help with saving an image
    By Manifesto32 in forum New To Java
    Replies: 2
    Last Post: 04-01-2011, 04:35 AM
  3. saving an image
    By shakeel in forum Java 2D
    Replies: 1
    Last Post: 12-25-2010, 10:08 AM
  4. Error in saving an Image????
    By LankanSniper in forum Java 2D
    Replies: 4
    Last Post: 11-07-2009, 04:48 AM
  5. Saving image to sever.
    By dudejonne in forum New To Java
    Replies: 5
    Last Post: 11-03-2009, 02:01 PM

Posting Permissions

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