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 9 of 9

Thread: Reading A Simple Image

  1. #1
    Junior Member bulbasaur's Avatar
    Join Date
    Dec 2020
    Location
    Alabama
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Reading A Simple Image

    Suppose I have a small jpg file (number.jpg), and it is a picture of the number 1. I want to write a program that parses reads this image and outputs the letter 'A' in my IDE console. How would I go about this?
    Last edited by bulbasaur; December 6th, 2020 at 05:29 PM. Reason: Reading an image is easier than parsing...

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Parsing A Simple Image

    What do you mean by "parses the image"?
    What does the image's contents: picture of a number have to do with outputing 'A'?
    What do you mean by "output the letter 'A'"?
    Is this code supposed to interact with the IDE to do the output on the IDE's console?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member bulbasaur's Avatar
    Join Date
    Dec 2020
    Location
    Alabama
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Parsing A Simple Image

    What do you mean by "parses the image"?
    Definitely not anything complicated. By parse I just mean open the image file, read that it's the number 1, and perhaps store it in a variable called num. If num is 0, then output could be 0. Else if the file is read successfully, 1 should be stored in the variable. Then if num is 1, output 'A'. Really hope this makes sense.

    What does the image's contents: picture of a number have to do with outputing 'A'?
    Basically (and in the future), I want to create a program that reads and interprets data from an image file.

    What do you mean by "output the letter 'A'"?
    Like, when you press compile in Eclipse, it opens the file, sees that it's a picture of the number 1, and prints out the char or string 'A'.

    Is this code supposed to interact with the IDE to do the output on the IDE's console?
    Hmm, I'm not sure, but the output 'A' should at least be presented in the IDE's console.
    Last edited by bulbasaur; December 6th, 2020 at 11:45 AM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Parsing A Simple Image

    read that it's the number 1,
    How will you do that? If you mean to look at the image's pixels and determine what the image shows, that is a very difficult task.
    Or do you have an OCR package that will do that for you?

    when you press compile in Eclipse, it opens the file, sees that it's a picture of the number 1, and outputs the letter 'A'.
    I'm not sure what that means. Why does Eclipse open a file when you tell Eclipse to do a compile?
    Are you talking about what your program will do? Executing a java program does NOT require an IDE. The java command can be used to execute java programs.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member bulbasaur's Avatar
    Join Date
    Dec 2020
    Location
    Alabama
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Parsing A Simple Image

    I hope this is not a difficult task.

    How will you do that?
    Suppose I have two image files and they have the same pixels of the number one. I want to compare the pixels of image 1 to see if they match the pixels in image 2.

    IF pixels in both images match, then the number 1 should be stored in the variable num. So basically, I compare the pixels two images. Would this be a difficult task to accomplish?


    Also, I was talking about what my program would do. I compile the code using Eclipse, which builds the program and shows a console output

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Parsing A Simple Image

    I want to compare the pixels of image 1 to see if they match the pixels in image 2.
    Look at the BufferedImage class. It has methods for accessing the pixels in an image.
    If the images are exact copies, then comparing the pixels of each should be straight forward. Otherwise the compare may be harder depending on how much differences there are.

    An IDE is not needed to compile and execute a java program. It is a tool that makes the job easier by making suggestions and taking care of some details.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member bulbasaur's Avatar
    Join Date
    Dec 2020
    Location
    Alabama
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Parsing A Simple Image

    I’ll see what I can do, then post an update.

  8. #8
    Junior Member bulbasaur's Avatar
    Join Date
    Dec 2020
    Location
    Alabama
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reading A Simple Image

    So this is a simple way to compare two images without the use of the BufferedImage class.

    package projects;
     
    import java.awt.Image;
    import java.awt.Toolkit;
    import java.awt.image.PixelGrabber;
     
    public class Program {
     
    	static void processImage() {
                    //Change file name if necessary
     
    		String file1 = "pic1.jpg"; 
    	        String file2 = "pic2.jpg";
     
    		// Load the images
    	        Image image1 = Toolkit.getDefaultToolkit().getImage(file1);
    		Image image2 = Toolkit.getDefaultToolkit().getImage(file2);
     
    		try {
     
    			PixelGrabber grabImage1Pixels = new PixelGrabber(image1, 0, 0, -1,
    					-1, false);
    			PixelGrabber grabImage2Pixels = new PixelGrabber(image2, 0, 0, -1,
    					-1, false);
     
    			int[] image1Data = null;
     
    			if (grabImage1Pixels.grabPixels()) {
    				int width = grabImage1Pixels.getWidth();
    				int height = grabImage1Pixels.getHeight();
    				image1Data = new int[width * height];
    				image1Data = (int[]) grabImage1Pixels.getPixels();
    			}
     
    			int[] image2Data = null;
     
    			if (grabImage2Pixels.grabPixels()) {
    				int width = grabImage2Pixels.getWidth();
    				int height = grabImage2Pixels.getHeight();
    				image2Data = new int[width * height];
    				image2Data = (int[]) grabImage2Pixels.getPixels();
    			}
     
    			System.out.println("Pixels equal: "
    					+ java.util.Arrays.equals(image1Data, image2Data));
     
    		} catch (InterruptedException e1) {
    			e1.printStackTrace();
    		}
    	}
     
    	public static void main(String args[]) {
    		processImage();
    	}
    }
    (Credit: https://www.javainuse.com/java/ImageEquals)

    I tested this and it runs in Eclipse as long as the image files, pic1.jpg and pic2.jpg, are in the correct folder.

    Also, I'm working on my first serious Java project, which is basically a program that solves an online Sudoku puzzle. What are some ideas on how to go about this? I don't know if I should be comparing image files in order to solve the puzzle.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Reading A Simple Image

    Sorry, I do not know anything about solving an online Sudoku puzzle.
    Once you get the algorithm for solving the puzzle and start trying to write code for it, post any questions here you have about writing java code.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How to show the output image (using java) as resultant image in webpage
    By Mumpy Zinu in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: February 24th, 2014, 08:00 AM
  2. Selecting and dragging a image from multiple image in Java Applet
    By CY5 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 26th, 2013, 02:44 PM
  3. Adding image to my simple window
    By TP-Oreilly in forum AWT / Java Swing
    Replies: 6
    Last Post: December 8th, 2011, 04:50 PM
  4. Simple Google Maps like Image viewer in Java
    By javaguy2020 in forum What's Wrong With My Code?
    Replies: 26
    Last Post: December 7th, 2011, 11:46 PM
  5. Replies: 2
    Last Post: February 14th, 2011, 05:36 PM

Tags for this Thread