Created
March 27, 2018 19:34
-
-
Save brunorozendo/f30bd4bde022c6f275fe3f714cdd1ccd to your computer and use it in GitHub Desktop.
java image
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.awt.Color; | |
| import java.awt.Rectangle; | |
| import java.awt.Robot; | |
| import java.awt.image.BufferedImage; | |
| public class Teste { | |
| public static void main(String[] args) { | |
| try { | |
| Robot robot = new Robot(); | |
| Rectangle captureRect = new Rectangle(533, 533, 100, 40); | |
| BufferedImage image = robot.createScreenCapture(captureRect); | |
| int[][] imageArray = convertTo2DUsingGetRGB(image); | |
| Color colorPixel; | |
| for (int i = 0; i < imageArray.length; i++) { | |
| for (int j = 0; j < imageArray[i].length; j++) { | |
| colorPixel = new Color(imageArray[i][j]); | |
| System.out.println("map[" + i + "][" + j + "] = rgb(" + colorPixel.getRed() + "," + colorPixel.getGreen() | |
| + "," + colorPixel.getBlue() + ")"); | |
| } | |
| } | |
| } catch (Exception ex) { | |
| ex.printStackTrace(); | |
| } | |
| } | |
| private static int[][] convertTo2DUsingGetRGB(BufferedImage image) { | |
| int width = image.getWidth(); | |
| int height = image.getHeight(); | |
| int[][] result = new int[height][width]; | |
| for (int row = 0; row < height; row++) { | |
| for (int col = 0; col < width; col++) { | |
| result[row][col] = image.getRGB(col, row); | |
| } | |
| } | |
| return result; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment