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?
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.
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...
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.
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.
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.read that it's the number 1,
Or do you have an OCR package that will do that for you?
I'm not sure what that means. Why does Eclipse open a file when you tell Eclipse to do a compile?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'.
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.
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
Look at the BufferedImage class. It has methods for accessing the pixels in an image.I want to compare the pixels of image 1 to see if they match the pixels in image 2.
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.
I’ll see what I can do, then post an update.
So this is a simple way to compare two images without the use of the BufferedImage class.
(Credit: https://www.javainuse.com/java/ImageEquals)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(); } }
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.
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.