问题描述
我一直在编写用于批处理图像的程序。 图像有多种格式,但是全部为JPEG,TIFF或PNG。 我编写了以下用于存储和访问像素数据的类:
package imageReader;
import misc.NumberFunctions;
/**
* A class to store pixel data and provide access to some of the pixel's basic
* attributes
*
* @author Thomas
*
*/
public class Pixel {
/**
* The X-coordinate of the pixel The left-most column of the image is x=0
*/
private int x;
/**
* The Y-coordinate of the pixel The top row of the image is y=0
*/
private int y;
/**
* The red value of the pixel (from 0 to 255)
*/
private int r;
/**
* The green value of the pixel (from 0 to 255)
*/
private int g;
/**
* The blue value of the pixel (from 0 to 255)
*/
private int b;
/**
* The highest color value in the pixel, which determine its brightness
* (from 0 to 255)
*/
private int value;
/**
* Creates an empty pixel object, setting it to black (0,0,0)
*
* @param x
* the x-coordinate of the pixel
* @param y
* the y-coordinate of the pixel
*/
public Pixel(int x, int y) {
this.x = x;
this.y = y;
this.r = 0;
this.g = 0;
this.b = 0;
this.setValue();
}
/**
* Get the x-coordinate of the pixel
*
* @return the x-coordinate
*/
public int getX() {
return this.x;
}
/**
* Get the y-coordinate of the pixel
*
* @return the y-coordinate
*/
public int getY() {
return this.y;
}
/**
* Get the red value of the pixel
*
* @return the red value (0 - 255)
*/
public int getRed() {
return this.r;
}
/**
* Get the green value of the pixel
*
* @return the green value (0 - 255)
*/
public int getGreen() {
return this.g;
}
/**
* Get the blue value of the pixel
*
* @return the blue value (0 - 255)
*/
public int getBlue() {
return this.b;
}
/**
* Get the brightness of the pixel
*
* @return the value (0 - 255)
*/
public int getValue() {
return this.value;
}
/**
* Get the brightness of the pixel
*
* @return the value (0 - 100)
*/
public double getValue100() {
double value100 = ((double) this.value) / 2.55;
return value100;
}
/**
* Check if this Pixel object is set to the same location as another
*
* @param otherPixel
* the other pixel
* @return whether they share the same coordinates or not
*/
public boolean hasSameLocation(Pixel otherPixel) {
boolean xMatches = (this.getX() == otherPixel.getX());
boolean yMatches = (this.getY() == otherPixel.getY());
return (xMatches && yMatches);
}
/**
* Checks if this pixel has the same color as another pixel
*
* @param otherPixel
* the other pixel
* @return whether they share the same color or not
*/
public boolean hasSameColor(Pixel otherPixel) {
if (this.getValue() != otherPixel.getValue())
return false;
boolean redMatches = (this.getRed() == otherPixel.getRed());
boolean greenMatches = (this.getGreen() == otherPixel.getGreen());
boolean blueMatches = (this.getBlue() == otherPixel.getBlue());
if (redMatches && greenMatches && blueMatches)
return true;
else
return false;
}
/**
* An internal function for finding the brightness value of the pixel
*/
private void setValue() {
this.value = NumberFunctions.getMax(r, g, b);
}
}
我想导入图像(我已经可以获取所有图像的路径),然后将每个图像的数据记录在Pixel对象的二维数组中(Pixel [] [])。 为此,我需要你们的帮助来做三件事:
确定图像类型并导入
找出图像的尺寸
读取每个像素的RGB值
我感谢你们能给我任何帮助。 谢谢!
1楼
使用BufferedImage
:
try {
BufferedImage image = ImageIO.read(new File("monkey.jpg")); // TODO actual file
int width = image.getWidth();
int height = image.getHeight();
// TODO do whatever you want with image, for example get an rgb:
Color color = new Color(image.getRgb(0,0));
} catch (IOException e) {
// TODO handle
}
顺便说一下,这是丑陋的代码:
if (redMatches && greenMatches && blueMatches)
return true;
else
return false;
替换为:
return redMatches && greenMatches && blueMatches;
2楼
您可以使用此代码导入给定文件名的图像。 这可能会引发IOException,因此请注意这一点。
String path = "Image1.jpg";
File file = new File(path);
BufferedImage image = ImageIO.read(file);
图像类型对此并不重要, ImageIO
将自动检测它。
如果要在阅读后输入文字,请使用image.getType()
图像尺寸可以从BufferedImage
int width = image.getWidth();
int height = image.getHeight();
可以使用image.getRGB(int x, int y)
读取像素,该像素返回整数颜色。
您可以使用构造函数Color c = new Color(int rgb)
将其转换为java.awt.color
。然后使用c.getRed()
c.getGreen()
c.getBlue(0)
获取通道是一个简单的过程。
如果我可以给您一些建议,请不要重复数据。
无需构建Pixel
,只需编写您的方法即可使用上述方法获取数据。
正如其他人指出的那样,您的实现将占用更多的内存。