希望
if (rgb[0] + rgb[1] + rgb[2] >= THRESOLD) {
bosS.write(( + i + " " + j + " 1 " + "\r").getBytes());
} else {
bosS.write(( + i + " " + j + " 0 " + "\r").getBytes());
}
输出结果
这段输出的结果能换行
变成这个样子import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;import javax.imageio.ImageIO;public class ReadColorTest {
private static final String DIR_PATH = "E:\\20 mi";//图片文件夹
private static final String TXT_PATH = "E:\\20 mi txt";//txt文件夹
private static final String COUNTER_PATH = TXT_PATH + "\\counter.txt";//计数txt
private static final int N = 100;// 阈值
private static final String THRESOLD_PATH="E:\\20 mithresold";//s txt 文件夹
private static final int THRESOLD=100;//设定的A值
/**
* 获取不带后缀名的文件名
*
* @param filename
* @return
*/
public String getname(String filename) {
int index = filename.lastIndexOf(".");
if ((index > -1) && (index < filename.length() - 1)) {
return filename.substring(0, index);
}
return filename;
} /**
* 读取一张图片的RGB值
*
* @throws Exception
*/
public int[] getImagePixel(String image) throws Exception {
String filename = getname(image);
File fileCar = new File(TXT_PATH + "\\" + filename + ".txt");
FileOutputStream fos = new FileOutputStream(fileCar);
BufferedOutputStream bos = new BufferedOutputStream(fos); ///输出S 的输出流
File fileS=new File(THRESOLD_PATH+"\\"+filename+".txt");
FileOutputStream fosS = new FileOutputStream(fileS);
BufferedOutputStream bosS = new BufferedOutputStream(fosS); int[] rgb = new int[3];
File file = new File(DIR_PATH + "\\" + 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(); //计数器
int[] counter = new int[2];
counter[0] = counter[1] = 0;//初始化
System.out.println("width=" + width + ",height=" + height + ".");
bos.write(("width=" + width + ",height=" + height + ".\n").getBytes());
System.out.println("minx=" + minx + ",miniy=" + miny + ".");
bos.write(("minx=" + minx + ",miniy=" + miny + ".\n").getBytes());
for (int i = minx; i < width; i++) {
for (int j = miny; j < height; j++) {
int pixel = bi.getRGB(i, j); // 下面三行代码将一个数字转换为RGB数字
rgb[0] = (pixel & 0xff0000) >> 16;
rgb[1] = (pixel & 0xff00) >> 8;
rgb[2] = (pixel & 0xff);
System.out.println("i=" + i + ",j=" + j + ":(" + rgb[0] + "," + rgb[1] + "," + rgb[2] + ")");
bos.write(("i=" + i + ",j=" + j + ":(" + rgb[0] + "," + rgb[1] + "," + rgb[2] + ")\n").getBytes());
if (rgb[0] + rgb[1] + rgb[2] >= N) {
counter[0] = counter[0] + 1;
} else {
counter[1] = counter[1] + 1;
}
//判定S
if (rgb[0] + rgb[1] + rgb[2] >= THRESOLD) {
bosS.write(( + i + " " + j + " 1 " + "\r").getBytes());
} else {
bosS.write(( + i + " " + j + " 0 " + "\r").getBytes());
} }
}
bos.flush();
bos.close();
fos.flush();
fos.close();
bosS.flush();
bosS.close();
fosS.flush();
fosS.close();
return counter;
} /**
* 返回屏幕色彩值
*
* @param x
* @param y
* @return
* @throws AWTException
*/
public int getScreenPixel(int x, int y) throws AWTException { // 函数返回值为颜色的RGB值。
Robot rb = null; // java.awt.image包中的类,可以用来抓取屏幕,即截屏。
rb = new Robot();
Toolkit tk = Toolkit.getDefaultToolkit(); // 获取缺省工具包
Dimension di = tk.getScreenSize(); // 屏幕尺寸规格
System.out.println(di.width);
System.out.println(di.height);
Rectangle rec = new Rectangle(0, 0, di.width, di.height);
BufferedImage bi = rb.createScreenCapture(rec);
int pixelColor = bi.getRGB(x, y); return 16777216 + pixelColor; // pixelColor的值为负,经过实践得出:加上颜色最大值就是实际颜色值。
} /**
* 读取DIR_PATH 下面的文件名
*
* @return
*/
public String[] getImagePath() {
try {
File file = new File(DIR_PATH);
if (file.isDirectory()) {
return file.list();
}
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
return null;
}
/**
* @param args
*/
public static void main(String[] args) throws Exception {
int x = 0;
ReadColorTest rc = new ReadColorTest();
x = rc.getScreenPixel(100, 345);
System.out.println(x + " - ");
String[] filelist = rc.getImagePath();
File counterFile = new File(COUNTER_PATH);
if (!counterFile.exists()) {
counterFile.createNewFile();
}
FileOutputStream fos = new FileOutputStream(counterFile);
BufferedOutputStream bos = new BufferedOutputStream(fos);
bos.write(("图片序号,小与N的点数,大于N的点数\r\n").getBytes());
if (filelist != null && filelist.length > 0) {
for (String filename : filelist) {
int[] counter = rc.getImagePixel(filename);
bos.write((filename + "," + counter[1] + "," + counter[0] + "\r\n").getBytes());
}
}
bos.flush();
bos.close();
fos.flush();
fos.close(); }}
if (rgb[0] + rgb[1] + rgb[2] >= THRESOLD) {
bosS.write(( + i + " " + j + " 1 " + "\r").getBytes());
} else {
bosS.write(( + i + " " + j + " 0 " + "\r").getBytes());
}
输出结果
这段输出的结果能换行
变成这个样子import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;import javax.imageio.ImageIO;public class ReadColorTest {
private static final String DIR_PATH = "E:\\20 mi";//图片文件夹
private static final String TXT_PATH = "E:\\20 mi txt";//txt文件夹
private static final String COUNTER_PATH = TXT_PATH + "\\counter.txt";//计数txt
private static final int N = 100;// 阈值
private static final String THRESOLD_PATH="E:\\20 mithresold";//s txt 文件夹
private static final int THRESOLD=100;//设定的A值
/**
* 获取不带后缀名的文件名
*
* @param filename
* @return
*/
public String getname(String filename) {
int index = filename.lastIndexOf(".");
if ((index > -1) && (index < filename.length() - 1)) {
return filename.substring(0, index);
}
return filename;
} /**
* 读取一张图片的RGB值
*
* @throws Exception
*/
public int[] getImagePixel(String image) throws Exception {
String filename = getname(image);
File fileCar = new File(TXT_PATH + "\\" + filename + ".txt");
FileOutputStream fos = new FileOutputStream(fileCar);
BufferedOutputStream bos = new BufferedOutputStream(fos); ///输出S 的输出流
File fileS=new File(THRESOLD_PATH+"\\"+filename+".txt");
FileOutputStream fosS = new FileOutputStream(fileS);
BufferedOutputStream bosS = new BufferedOutputStream(fosS); int[] rgb = new int[3];
File file = new File(DIR_PATH + "\\" + 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(); //计数器
int[] counter = new int[2];
counter[0] = counter[1] = 0;//初始化
System.out.println("width=" + width + ",height=" + height + ".");
bos.write(("width=" + width + ",height=" + height + ".\n").getBytes());
System.out.println("minx=" + minx + ",miniy=" + miny + ".");
bos.write(("minx=" + minx + ",miniy=" + miny + ".\n").getBytes());
for (int i = minx; i < width; i++) {
for (int j = miny; j < height; j++) {
int pixel = bi.getRGB(i, j); // 下面三行代码将一个数字转换为RGB数字
rgb[0] = (pixel & 0xff0000) >> 16;
rgb[1] = (pixel & 0xff00) >> 8;
rgb[2] = (pixel & 0xff);
System.out.println("i=" + i + ",j=" + j + ":(" + rgb[0] + "," + rgb[1] + "," + rgb[2] + ")");
bos.write(("i=" + i + ",j=" + j + ":(" + rgb[0] + "," + rgb[1] + "," + rgb[2] + ")\n").getBytes());
if (rgb[0] + rgb[1] + rgb[2] >= N) {
counter[0] = counter[0] + 1;
} else {
counter[1] = counter[1] + 1;
}
//判定S
if (rgb[0] + rgb[1] + rgb[2] >= THRESOLD) {
bosS.write(( + i + " " + j + " 1 " + "\r").getBytes());
} else {
bosS.write(( + i + " " + j + " 0 " + "\r").getBytes());
} }
}
bos.flush();
bos.close();
fos.flush();
fos.close();
bosS.flush();
bosS.close();
fosS.flush();
fosS.close();
return counter;
} /**
* 返回屏幕色彩值
*
* @param x
* @param y
* @return
* @throws AWTException
*/
public int getScreenPixel(int x, int y) throws AWTException { // 函数返回值为颜色的RGB值。
Robot rb = null; // java.awt.image包中的类,可以用来抓取屏幕,即截屏。
rb = new Robot();
Toolkit tk = Toolkit.getDefaultToolkit(); // 获取缺省工具包
Dimension di = tk.getScreenSize(); // 屏幕尺寸规格
System.out.println(di.width);
System.out.println(di.height);
Rectangle rec = new Rectangle(0, 0, di.width, di.height);
BufferedImage bi = rb.createScreenCapture(rec);
int pixelColor = bi.getRGB(x, y); return 16777216 + pixelColor; // pixelColor的值为负,经过实践得出:加上颜色最大值就是实际颜色值。
} /**
* 读取DIR_PATH 下面的文件名
*
* @return
*/
public String[] getImagePath() {
try {
File file = new File(DIR_PATH);
if (file.isDirectory()) {
return file.list();
}
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
return null;
}
/**
* @param args
*/
public static void main(String[] args) throws Exception {
int x = 0;
ReadColorTest rc = new ReadColorTest();
x = rc.getScreenPixel(100, 345);
System.out.println(x + " - ");
String[] filelist = rc.getImagePath();
File counterFile = new File(COUNTER_PATH);
if (!counterFile.exists()) {
counterFile.createNewFile();
}
FileOutputStream fos = new FileOutputStream(counterFile);
BufferedOutputStream bos = new BufferedOutputStream(fos);
bos.write(("图片序号,小与N的点数,大于N的点数\r\n").getBytes());
if (filelist != null && filelist.length > 0) {
for (String filename : filelist) {
int[] counter = rc.getImagePixel(filename);
bos.write((filename + "," + counter[1] + "," + counter[0] + "\r\n").getBytes());
}
}
bos.flush();
bos.close();
fos.flush();
fos.close(); }}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货