给你一个java的,自己改改
import java.awt.*;
import java.awt.image.*;
import java.util.Random;
import java.io.*;
import javax.swing.*;
public class LoadImage {
/**
* @param args
*/
public static void main(String[] args) {
String myreadline = "";
//定义一个String类型的变量,用来每次读取一行
try {
FileReader fr = new FileReader("data/imagelist.txt");//创建FileReader对象,用来读取字符流
BufferedReader br = new BufferedReader(fr); //缓冲指定文件的输入
FileWriter fw = new FileWriter("data/17d_result.txt");//创建FileWriter对象,用来写入字符流
BufferedWriter bw = new BufferedWriter(fw); //将缓冲对文件的输出
while (br.ready()) {
myreadline = br.readLine();//读取一行
BufferedImage image = toBufferedImage(new ImageIcon("data/Image/"+myreadline).getImage());
int height = image.getHeight();
int width = image.getWidth();
int r1=0, r2=0, r3=0, r4=0, r5=0, r6=0, r7=0, r8=0, r9=0, r10=0, r11=0, r12=0, r13=0, r14=0, r15=0, r16=0, r17=0;
int g1=0, g2=0, g3=0, g4=0, g5=0, g6=0, g7=0, g8=0, g9=0, g10=0, g11=0, g12=0, g13=0, g14=0, g15=0, g16=0, g17=0;
int b1=0, b2=0, b3=0, b4=0, b5=0, b6=0, b7=0, b8=0, b9=0, b10=0, b11=0, b12=0, b13=0, b14=0, b15=0, b16=0, b17=0;
int rgb1=0, rgb2=0, rgb3=0, rgb4=0, rgb5=0, rgb6=0, rgb7=0, rgb8=0, rgb9=0, rgb10=0, rgb11=0, rgb12=0, rgb13=0, rgb14=0, rgb15=0, rgb16=0, rgb17=0;
//System.out.println("Height=" + height + ", Width=" + width);
//Random ran = new Random();
//int x = ran.nextInt(width), y = ran.nextInt(height);
for (int y=0;y<height;y++) {
for (int x=0;x<width;x++) {
Color color = new Color(image.getRGB(x, y));
if(color.getRed()<=15)
r1++;
if(color.getRed()>15 && color.getRed()<=30)
r2++;
if(color.getRed()>30 && color.getRed()<=45)
r3++;
if(color.getRed()>45 && color.getRed()<=60)
r4++;
if(color.getRed()>60 && color.getRed()<=75)
r5++;
if(color.getRed()>75 && color.getRed()<=90)
r6++;
if(color.getRed()>90 && color.getRed()<=105)
r7++;
if(color.getRed()>105 && color.getRed()<=120)
r8++;
if(color.getRed()>120 && color.getRed()<=135)
r9++;
if(color.getRed()>135 && color.getRed()<=150)
r10++;
if(color.getRed()>150 && color.getRed()<=165)
r11++;
if(color.getRed()>165 && color.getRed()<=180)
r12++;
if(color.getRed()>180 && color.getRed()<=195)
r13++;
if(color.getRed()>195 && color.getRed()<=210)
r14++;
if(color.getRed()>210 && color.getRed()<=225)
r15++;
if(color.getRed()>225 && color.getRed()<=240)
r16++;
if(color.getRed()>240 && color.getRed()<=255)
r17++;
if(color.getGreen()<=15)
g1++;
if(color.getGreen()>15 && color.getGreen()<=30)
g2++;
if(color.getGreen()>30 && color.getGreen()<=45)
g3++;
if(color.getGreen()>45 && color.getGreen()<=60)
g4++;
if(color.getGreen()>60 && color.getGreen()<=75)
g5++;
if(color.getGreen()>75 && color.getGreen()<=90)
g6++;
if(color.getGreen()>90 && color.getGreen()<=105)
g7++;
if(color.getGreen()>105 && color.getGreen()<=120)
g8++;
if(color.getGreen()>120 && color.getGreen()<=135)
g9++;
if(color.getGreen()>135 && color.getGreen()<=150)
g10++;
if(color.getGreen()>150 && color.getGreen()<=165)
g11++;
if(color.getGreen()>165 && color.getGreen()<=180)
g12++;
if(color.getGreen()>180 && color.getGreen()<=195)
g13++;
if(color.getGreen()>195 && color.getGreen()<=210)
g14++;
if(color.getGreen()>210 && color.getGreen()<=225)
g15++;
if(color.getGreen()>225 && color.getGreen()<=240)
g16++;
if(color.getGreen()>240 && color.getGreen()<=255)
g17++;
if(color.getBlue()<=15)
b1++;
if(color.getBlue()>15 && color.getBlue()<=30)
b2++;
if(color.getBlue()>30 && color.getBlue()<=45)
b3++;
if(color.getBlue()>45 && color.getBlue()<=60)
b4++;
if(color.getBlue()>60 && color.getBlue()<=75)
b5++;
if(color.getBlue()>75 && color.getBlue()<=90)
b6++;
if(color.getBlue()>90 && color.getBlue()<=105)
b7++;
if(color.getBlue()>105 && color.getBlue()<=120)
b8++;
if(color.getBlue()>120 && color.getBlue()<=135)
b9++;
if(color.getBlue()>135 && color.getBlue()<=150)
b10++;
if(color.getBlue()>150 && color.getBlue()<=165)
b11++;
if(color.getBlue()>165 && color.getBlue()<=180)
b12++;
if(color.getBlue()>180 && color.getBlue()<=195)
b13++;
if(color.getBlue()>195 && color.getBlue()<=210)
b14++;
if(color.getBlue()>210 && color.getBlue()<=225)
b15++;
if(color.getBlue()>225 && color.getBlue()<=240)
b16++;
if(color.getBlue()>240 && color.getBlue()<=255)
b17++;
}
}
rgb1 = r1 + g1 + b1;
rgb2 = r2 + g2 + b2;
rgb3 = r3 + g3 + b3;
rgb4 = r4 + g4 + b4;
rgb5 = r5 + g5 + b5;
rgb6 = r6 + g6 + b6;
rgb7 = r7 + g7 + b7;
rgb8 = r8 + g8 + b8;
rgb9 = r9 + g9 + b9;
rgb10 = r10 + g10 + b10;
rgb11 = r11 + g11 + b11;
rgb12 = r12 + g12 + b12;
rgb13 = r13 + g13 + b13;
rgb14 = r14 + g14 + b14;
rgb15 = r15 + g15 + b15;
rgb16 = r16 + g16 + b16;
rgb17 = r17 + g17 + b17;
//System.out.println("rect " + rgb1 + " " + rgb2 + " " + rgb3);
bw.write("rect " + rgb1 + " " + rgb2 + " " + rgb3 + " " + rgb4 + " " + rgb5 + " " + rgb6 + " " + rgb7 + " " + rgb8 + " " + rgb9 + " " + rgb10 + " " + rgb11 + " " + rgb12 + " " + rgb13 + " " + rgb14 + " " + rgb15 + " " + rgb16 + " " + rgb17); //写入文件
bw.newLine();
//System.out.println(myreadline);//在屏幕上输出
}
bw.flush(); //刷新该流的缓冲
bw.close();
br.close();
fw.close();
br.close();
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// This method returns a buffered image with the contents of an image
public static BufferedImage toBufferedImage(Image image) {
if (image instanceof BufferedImage) {
return (BufferedImage) image;
}
// Determine if the image has transparent pixels; for this method's
// implementation, see e661 Determining If an Image Has Transparent
// Pixels
boolean hasAlpha = hasAlpha(image);
// Create a buffered image with a format that's compatible with the
// screen
BufferedImage bimage = null;
GraphicsEnvironment ge = GraphicsEnvironment
.getLocalGraphicsEnvironment();
try {
// Determine the type of transparency of the new buffered image
int transparency = Transparency.OPAQUE;
if (hasAlpha) {
transparency = Transparency.BITMASK;
}
// Create the buffered image
GraphicsDevice gs = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gs.getDefaultConfiguration();
bimage = gc.createCompatibleImage(image.getWidth(null), image
.getHeight(null), transparency);
} catch (HeadlessException e) {
// The system does not have a screen
}
if (bimage == null) {
// Create a buffered image using the default color model
int type = BufferedImage.TYPE_INT_RGB;
if (hasAlpha) {
type = BufferedImage.TYPE_INT_ARGB;
}
bimage = new BufferedImage(image.getWidth(null), image
.getHeight(null), type);
}
// Copy image to buffered image
Graphics g = bimage.createGraphics();
// Paint the image onto the buffered image
g.drawImage(image, 0, 0, null);
g.dispose();
return bimage;
}
// This method returns true if the specified image has transparent pixels
public static boolean hasAlpha(Image image) {
// If buffered image, the color model is readily available
if (image instanceof BufferedImage) {
BufferedImage bimage = (BufferedImage) image;
return bimage.getColorModel().hasAlpha();
}
// Use a pixel grabber to retrieve the image's color model;
// grabbing a single pixel is usually sufficient
PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);
try {
pg.grabPixels();
} catch (InterruptedException e) {
}
// Get the image's color model
ColorModel cm = pg.getColorModel();
return cm.hasAlpha();
}
}
温馨提示:内容为网友见解,仅供参考