我是Java编程的初学者。我有算法,但是无法编写代码。我想比较RGB十六进制像素值的位置2和4处的2张相似图像,以检查其中一幅图像的像素值是否不同于另一幅图像。
我的算法逻辑:if(image1.substring2 == image2.substring2) && (image1.substring4 == image2.substring4)
,像素相同。if(image1.substring2 != image2.substring2) || (image1.substring4 != image2.substring4)
,像素不一样。if(image1.substring2 != image2.substring2) && (image1.substring4 != image2.substring4)
,像素不一样。
我这里有剩余的代码。我试图将所有过程分开,以便以后进行故障排除时很容易。
//主要
public class getPixelRGB1
{
private static int a;
private static int r;
private static int g;
private static int b;
private static final double bitPerColor = 4.0;
public static void main(String[] args) throws IOException
{
FileInputStream image = null;
FileInputStream image2 = null;
getPixelData1 newPD = new getPixelData1();
try {
BufferedImage img, img2;
File file = new File("img0.jpg");
File file2 = new File("imgstega.jpg");
image = new FileInputStream(file);
image2 = new FileInputStream(file2);
img = ImageIO.read(image);
img2 = ImageIO.read(image2);
int rowcol;
int width = img.getWidth();
int height = img.getHeight();
System.out.println("Image's Width: " + width);
System.out.println("Image's Height: " + height);
int[][] pixelData = new int[width * height][3];
System.out.println("Pixel Data: " + pixelData);
int[] rgb;
int count = 0;
for(int i=0; i<width; i++)
{
for(int j=0; j<height; j++)
{
rgb = newPD.getPixelData(img, i, j);
for(int k = 0; k < rgb.length; k++)
{
pixelData[count][k] = rgb[k];
}
count++;
System.out.println("\nRGB Counts: " + count);
}
}
int width2 = img2.getWidth();
int height2 = img2.getHeight();
System.out.println("Image's Width: " + width2);
System.out.println("Image's Height: " + height2);
int[][] pixelData2 = new int[width2 * height2][3];
int[] rgb2;
int counter = 0;
for(int i=0; i<width2; i++)
{
for(int j=0; j<height2; j++)
{
rgb2 = newPD.getPixelData(img2, i, j);
for(int k = 0; k < rgb2.length; k++)
{
pixelData2[counter][k] = rgb2[k];
}
counter++;
System.out.println("\nRGB2 Counts: " + counter);
}
}
} catch (FileNotFoundException ex) {
Logger.getLogger(getPixelRGB1.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
image.close();
} catch (IOException ex) {
Logger.getLogger(getPixelRGB1.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
// 1ST流程-获取RGB像素值
public class getPixelData1
{
private static final double bitPerColor = 4.0;
public int[] getPixelData(BufferedImage img, int w, int h) throws IOException
{
int argb = img.getRGB(w, h);
int rgb[] = new int[]
{
(argb >> 16) & 0xff, //red
(argb >> 8) & 0xff, //green
(argb ) & 0xff //blue
};
int red = rgb[0];
int green = rgb[1]; //RGB Value in Decimal
int blue = rgb[2];
System.out.println("\nRGBValue in Decimal --> " + "\nRed: " + red + " Green: " + green + " Blue: " + blue);
//Convert each channel RGB to Hexadecimal value
String rHex = Integer.toHexString((int)(red));
String gHex = Integer.toHexString((int)(green));
String bHex = Integer.toHexString((int)(blue));
System.out.println("\nRGBValue in Hexa --> " + "\nRed Green Blue " + rHex + gHex + bHex);
//Check position 2 and 4 of hexa value for any changes
String hexa2, hexa4 = "";
String rgbHexa = rHex + gHex + bHex;
hexa2 = rgbHexa.substring(1,2);
System.out.println("\nString RGB Hexa: " + rgbHexa);
System.out.println("\nSubstring at position 2: " + hexa2);
String hex = String.format("%02X%02X%02X", red, green, blue);
hexa4 = hex.substring(3,4);
System.out.println("\nSubstring at position 4: " + hexa4);
return rgb;
}
}
//第二过程-比较两个图像的RGB Hex值
public class compareHexaRGB
{
public int[] compareHexaRGB(BufferedImage img, BufferedImage img2, int w, int h) throws IOException
{
getPixelData1 newPD = new getPixelData1(); //get method from class getPixelData1 - is this correct?
if((img.hexa2.equals(img2.hexa2)) && (img.hexa4.equals(img2.hexa4)))
{
System.out.println("Pixel values at position 2 and 4 are the same.");
}
else if((img.hexa2 != img2.hexa2) || (img.hexa4 != img2.hexa4))
{
System.out.println("Pixel values at position 2 and 4 are not the same.");
}
else if((img.hexa2 != img2.hexa2) && (img.hexa4 != img2.hexa4))
{
System.out.println("Pixel values at position 2 and 4 are not the same.");
}
}
}
该错误表明程序无法在bufferedImage中找到hexa2和hexa4的符号。有人可以在这里检查我的编码是否做错了吗?我还是Java的新手。
最佳答案
img
的类型为BufferedImage
(javadoc)。它不包含任何名为hexa2
或hexa4
的非私有(非私有)字段。
您需要做的是重构代码,以确保您可以在compareHexaRGB()
中访问它们。可能有很多方法可以完成。也许您可以扩展BufferedImage
以包括您的字段,或者您可以将它们作为方法的输入来传递。
鉴于我们实际上并没有您的所有代码,因此很难确定哪种解决方案更为优雅(例如,我根本看不到compareHexaRGB()
被调用)。
要更精确地说明编译问题:通过使用img.hexa2
访问字段,您假定hexa2
中存在一个名为BufferedImage
的字段,您可以从类中访问该字段。如果将某个字段声明为public
,则为true。更典型地,这些字段是private
范围的,并且需要由getter / setter访问。对于BufferedImage
,根本没有这样的字段。
了解访问控制here。