public class Main {
private static final String OUTPUT_FORMAT = "%3d:(%3d,%3d,%3d)\t";
public static void main(String[] args) {
getImagePixel("D:\\log\\pic\\11111.jpg");
}
public static void getImagePixel(String image) {
int[] rgb = new int[3];
File file = new File(image);
BufferedImage bi = null;
try {
bi = ImageIO.read(file);
} catch (Exception e) {
e.printStackTrace();
}
int width = bi.getWidth();
int height = bi.getHeight();
int minx = bi.getMinX();
int miny = bi.getMinY();
System.out.println("width=" + width + ",height=" + height + ".");
System.out.println("minx=" + minx + ",miniy=" + miny + ".");
FileOutputStream out = null;
try {
out = new FileOutputStream(new File(".\\test.txt"));
for (int i = minx; i < height; i++) {
for (int j = miny; j < width; j++) {
int pixel = bi.getRGB(j, i);
rgb[0] = (pixel & 0xff0000) >> 16;
rgb[1] = (pixel & 0xff00) >> 8;
rgb[2] = (pixel & 0xff);
String unit = String.format(OUTPUT_FORMAT, j, rgb[0], rgb[1], rgb[2]);
out.write(unit.getBytes());
}
out.write("\r\n".getBytes());
}
} catch (Exception e) {
Log.e(TAG, "Fail to write file.");
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
}
}
}