Page 1 of 2 12 LastLast
Results 1 to 20 of 37
Like Tree1Likes

Thread: How To replace specific area color of image

  1. #1
    raj.mscking@gmail.com's Avatar
    raj.mscking@gmail.com is offline Senior Member
    Join Date
    Jan 2012
    Location
    TamilNadu
    Posts
    290
    Rep Power
    3

    Default How To replace specific area color of image

    Hi!

    I need your guide in my new project, i don't have lot of experience on image handling,

    I am trying to custom graphic class..,

    My task is to replace the specific area color of the image when i select the area & pick the color..,

    If i choose the hand / body of the T-shirts means the color can change by the user..,

    from How To replace specific area color of image-.png To How To replace specific area color of image-tw.png

    I am looking for your guide..,

    Just give me a idea i may try for it please.., any other library is their for manage this..,

    Thank You!

    Have A Happy Day..,
    - Raj

  2. #2
    raj.mscking@gmail.com's Avatar
    raj.mscking@gmail.com is offline Senior Member
    Join Date
    Jan 2012
    Location
    TamilNadu
    Posts
    290
    Rep Power
    3

    Default Re: How To replace specific area color of image

    Hi!

    Anyone Guide me please..,
    - Raj

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,025
    Rep Power
    6

    Default Re: How To replace specific area color of image

    Check out FilteredImageSource (Java Platform SE 8 ). They provide a hint as to how you might start it. You can also search the web for an existing solution if you choose.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    raj.mscking@gmail.com's Avatar
    raj.mscking@gmail.com is offline Senior Member
    Join Date
    Jan 2012
    Location
    TamilNadu
    Posts
    290
    Rep Power
    3

    Default Re: How To replace specific area color of image

    Thank You very much Mr.jim829!

    Thanks for your idea, i try and will come with successive result..,

    Have A Happy Day..,
    - Raj

  5. #5
    raj.mscking@gmail.com's Avatar
    raj.mscking@gmail.com is offline Senior Member
    Join Date
    Jan 2012
    Location
    TamilNadu
    Posts
    290
    Rep Power
    3

    Default Re: How To replace specific area color of image

    Hi!

    It is working fine with FilteredImageSource.

    Thank You!

    How to list-out the colors presented on the image? (like yellow, white colors of the above T-shirt image)..,

    is their any method available in image package..,

    Thank You!

    Have A Happy Day..,
    - Raj

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,025
    Rep Power
    6

    Default Re: How To replace specific area color of image

    Quote Originally Posted by raj.mscking@gmail.com View Post

    How to list-out the colors presented on the image? (like yellow, white colors of the above T-shirt image)..,

    is their any method available in image package..,
    Not that I am aware of. You could do it pixel by pixel and put it in a Map<Integer,List<Point>> or something similar where each entry has all the points represented by the color. I don't believe that would work well unless there were just a few colors and they were solid. Think about a picture which has red and variants of red to offer different shading. You would need to not only change the red but also correct the shading to match. So additional logic in the code would be required.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    raj.mscking@gmail.com's Avatar
    raj.mscking@gmail.com is offline Senior Member
    Join Date
    Jan 2012
    Location
    TamilNadu
    Posts
    290
    Rep Power
    3

    Default Re: How To replace specific area color of image

    Thank you Mr.jim829!

    i need some clarification in this. In Map<Integer,List<Point>> in this collection (Integer, List & point are represent pixel or what Mr.Jim)..,

    really I am very thanks for your guide Mr.Jim..,

    Have A Happy Day..,
    - Raj

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: How To replace specific area color of image

    Since Jim won't be up for a little bit, the Integer is the colour, and the List<Point> is the list of pixels with that colour.
    If you just want the colours you could simply go with Set<Integer>.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    raj.mscking@gmail.com's Avatar
    raj.mscking@gmail.com is offline Senior Member
    Join Date
    Jan 2012
    Location
    TamilNadu
    Posts
    290
    Rep Power
    3

    Default Re: How To replace specific area color of image

    Thank You Mr.Tolls!


    My working code is (To change the image color value by manual input)

    Java Code:
    /**
     *
     * @author Raj
     */
    
    import java.awt.Component;
    import java.awt.Frame;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.Toolkit;
    import java.awt.image.FilteredImageSource;
    import java.awt.image.RGBImageFilter;
    
    import javax.swing.ImageIcon;
    
    public class RGBSwapping {
    
      static Image image;
    
      public static void main(String[] args) {
    
    image = new ImageIcon("image.png").getImage();
    
    FilteredImageSource filteredImageSource = new FilteredImageSource(image.getSource(), new RedBlueSwapFilter());
    
    image = Toolkit.getDefaultToolkit().createImage(filteredImageSource);
    
    Frame frame = new Frame("Image Frame");
    
    frame.add(new CustomPaintComponent());
    
    int frameWidth = 300;
    
    int frameHeight = 300;
    
    frame.setSize(frameWidth, frameHeight);
    
    frame.setVisible(true);
    
      }
    
        static class CustomPaintComponent extends Component {
    
      public void paint(Graphics g) {
    
        Graphics2D g2d = (Graphics2D)g;
    
        int x = 0;
    
        int y = 0;
    
        g2d.drawImage(image, x, y, this);
    
      }
    
        }
    
    static class RedBlueSwapFilter extends RGBImageFilter 
    {
    
     public RedBlueSwapFilter() 
     {
        canFilterIndexColorModel = true;
    }
      public int filterRGB(int x, int y, int rgb) 
      {
       return ((rgb & 0xff00ff00) | ((rgb & 0xff0000) >> 16) | ((rgb & 0xff) << 16));
      }
      
     }
    
    }
    But i want to display the available colors of image as individually.
    If end user want to change he may change the color by color chooser..,

    from How To replace specific area color of image-one.jpg to How To replace specific area color of image-two.jpg

    Thank You Mr.Tolls!

    Have A Happy Day..,
    Attached Thumbnails Attached Thumbnails How To replace specific area color of image-two.jpg  
    Last edited by raj.mscking@gmail.com; 07-24-2014 at 12:45 PM.
    - Raj

  10. #10
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: How To replace specific area color of image

    Nicely done. May I lay down one final challenge?

    Remove that hardcoded path in your code.

    Java Code:
    image = new ImageIcon("C://Users//Android//Desktop//tw.png").getImage();
    Because that makes it so the application only works on a Windows Vista or higher computer with a user named 'Android'.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  11. #11
    raj.mscking@gmail.com's Avatar
    raj.mscking@gmail.com is offline Senior Member
    Join Date
    Jan 2012
    Location
    TamilNadu
    Posts
    290
    Rep Power
    3

    Default Re: How To replace specific area color of image

    Now is this ok Mr.Gimbal2..,
    - Raj

  12. #12
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: How To replace specific area color of image

    It is a little better, but you are now telling the application "load the file from the directory where the application is started".

    Try researching into using classpath resources for a way to load your file that just works everywhere and it also means you can just stick the image into your application.

    Accessing Resources
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  13. #13
    raj.mscking@gmail.com's Avatar
    raj.mscking@gmail.com is offline Senior Member
    Join Date
    Jan 2012
    Location
    TamilNadu
    Posts
    290
    Rep Power
    3

    Default Re: How To replace specific area color of image

    Sure mr.Jimbal2!

    but i am receiving the error while fallow..,

    Java Code:
    //Map Declaration 
    
      static Map<Integer,List<Point>> mp_list=new HashMap<Integer,List<Point>>();
      static List<Point> lst_point = new ArrayList<Point>();
      
    
     static class RedBlueSwapFilter extends RGBImageFilter 
    {
    
     public RedBlueSwapFilter() 
     {
        canFilterIndexColorModel = true;
    }
      public int filterRGB(int x, int y, int rgb) 
      {
      [B]    mp_list.put(rgb,lst_point.add(new Point(x, y)));[/B] //error line
        
       return ((rgb & 0xff00ff00) | ((rgb & 0xff0000) >> 16) | ((rgb & 0xff) << 16));
      }
      
     }
    error is :

    incompatible type boolean cannot to be converted into List<Point>
    - Raj

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: How To replace specific area color of image

    The add() method returns a boolean.
    Your code is trying to add that boolean to the Map.

    Note, every rgb colour in that map (assuming you fixed the above bug) would be using the same List...

    What you need to do in filterRGB is:
    Java Code:
    Get the List from the Map for that rgb value.
    If null then create a new List and add it to the Map.
    In either case add a new Point to the List.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  15. #15
    raj.mscking@gmail.com's Avatar
    raj.mscking@gmail.com is offline Senior Member
    Join Date
    Jan 2012
    Location
    TamilNadu
    Posts
    290
    Rep Power
    3

    Default Re: How To replace specific area color of image

    Hi!

    1 Get the List from the Map for that rgb value.
    2 If null then create a new List and add it to the Map.
    3 In either case add a new Point to the List.
    On the 1st line, in Map which list i have to check Key (or) value<Point>. i am getting confusion on this..,

    is this right

    Java Code:
    static class RedBlueSwapFilter extends RGBImageFilter 
    {
    
     public RedBlueSwapFilter() 
     {
        canFilterIndexColorModel = true;
    }
      public int filterRGB(int x, int y, int rgb) 
      {
          if(lst_point != null)
          {
              lst_point=new ArrayList<Point>();
              mp_list.put(rgb,lst_point.add(new Point(x, y)));
          }
           return ((rgb & 0xff00ff00) | ((rgb & 0xff0000) >> 16) | ((rgb & 0xff) << 16));
      }
      
     }
    Last edited by raj.mscking@gmail.com; 07-24-2014 at 03:01 PM.
    - Raj

  16. #16
    raj.mscking@gmail.com's Avatar
    raj.mscking@gmail.com is offline Senior Member
    Join Date
    Jan 2012
    Location
    TamilNadu
    Posts
    290
    Rep Power
    3

    Default Re: How To replace specific area color of image

    finally i have fallow as you said..,

    But Still now the line is showing error when put item on the map..,


    Java Code:
    static class RedBlueSwapFilter extends RGBImageFilter 
    {
    
     public RedBlueSwapFilter() 
     {
        canFilterIndexColorModel = true;
    }
      public int filterRGB(int x, int y, int rgb) 
      {
          
          List<Point> new_list=mp_list.get(rgb);
          if(new_list == null)
          {
              lst_point=new ArrayList<Point>();
              mp_list.put(rgb,lst_point.add(new Point(x, y)));
          }
           return ((rgb & 0xff00ff00) | ((rgb & 0xff0000) >> 16) | ((rgb & 0xff) << 16));
      }
      
     }
    I am looking for your help..,

    Thank You!

    Have A Happy Day..,
    - Raj

  17. #17
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: How To replace specific area color of image

    Let me change the todo list a little

    1. Get the List from the Map for that rgb value.
    2. If null then create a new List and add it to the Map.
    3. In either case add a new Point to the List.
    4. and then stick the list in the mp_list Map
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  18. #18
    raj.mscking@gmail.com's Avatar
    raj.mscking@gmail.com is offline Senior Member
    Join Date
    Jan 2012
    Location
    TamilNadu
    Posts
    290
    Rep Power
    3

    Default Re: How To replace specific area color of image

    Hi Mr.Jimbal2!

    i am new for this image handling. Step by step as before you said i am fallowing.

    But i don't know in which line i have to correct myself. Guide me please..,

    Java Code:
     public int filterRGB(int x, int y, int rgb) 
      {
          
          List<Point> new_list=mp_list.get(rgb);    // Get the List from the Map for that rgb value.
          if(new_list == null)                      // If null 
          {
              lst_point=new ArrayList<Point>();     // create a new List and add it to the Map.
              mp_list.put(rgb,lst_point.add(new Point(x, y))); // In either case add a new Point to the List.
          }
           return ((rgb & 0xff00ff00) | ((rgb & 0xff0000) >> 16) | ((rgb & 0xff) << 16));
      }
    - Raj

  19. #19
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: How To replace specific area color of image

    Oops, I misread the list myself. My additional 4th step is already in there.

    1. Get the List from the Map for that rgb value.
    2. If null then create a new List and add it to the Map. <--- your code does not do this
    3. In either case add a new Point to the List.

    No I'm not going to change anything for you. You think about it. It is in your best interest to not connect to this forum anymore until you've solved it because you are abusing this thread to not have to work it out yourself.

    understand what your code is doing. Then you can solve it.
    SurfMan likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  20. #20
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,087
    Rep Power
    4

    Default Re: How To replace specific area color of image

    This has nothing to do with image handling. This is a basic Java question about adding stuff to a List inside a Map considering it could be null:
    Java Code:
    List myList = myMap.get(someKey);   // Get the List from the Map 
    if ( myList == null) {              //if null THEN THE MAP DOES NOT CONTAIN THE LIST
       myList = new ArrayList();        //create a new List 
       myMap.put(someKey)               //and add it to the Map.
    }
    myList.add( something);              // In either case add something to the List.
    return whatever;
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013

Page 1 of 2 12 LastLast

Similar Threads

  1. Replace specific text with a link?
    By ChukMakeIt in forum GWT
    Replies: 5
    Last Post: 07-14-2012, 11:15 AM
  2. Replies: 0
    Last Post: 07-04-2012, 09:01 PM
  3. Replace a region of an image
    By alex88 in forum Java 2D
    Replies: 4
    Last Post: 06-04-2011, 06:21 PM
  4. Replies: 0
    Last Post: 03-01-2011, 08:57 AM
  5. How to change color of area contained by rectangle
    By thayalan in forum AWT / Swing
    Replies: 2
    Last Post: 06-04-2009, 05:48 AM

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
  •