【发布时间】:2022-11-03 10:07:05
【问题描述】:
我这里有一个名为 blackWhite 的缓冲区图像,它完全是黑色或白色。我在这里尝试做的是我获取一个二维像素坐标数组(在本例中为tog)并找出它是黑色还是白色,将其分配为黑色的0,如果不是,则将其保存回来到二维数组的第三列。虽然它没有出现任何错误,但它并没有给我正确的颜色,并且当我知道它不是白色时说它都是白色的。在这种情况下,我使用 DDA 来选择 2 个点击点之间的每个像素的坐标,这就是这里的数组 tog 中的内容。即使我只选择黑到黑像素,它仍然说一切都是白色的。
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
public class W3_Img_Process_Class_Only extends JPanel {
//--------------variables
int[][] tog; int step;
String inTitle="L1";
int imageCount=0;
int[] x= new int[2], y= new int[2];
int[] black= new int[2]; //-------------if black is 0, then the pixel is black
int clr; int flag=0;
private BufferedImage master;
private BufferedImage blackWhite;
public W3_Img_Process_Class_Only() {
//----------------------try/catch for (pure black || pure white)
try {
master = ImageIO.read(new File(Image_Path));
blackWhite = new BufferedImage(master.getWidth(), master.getHeight(), BufferedImage.TYPE_BYTE_BINARY);
Graphics2D g2d = blackWhite.createGraphics();
g2d.drawImage(master, 0, 0, this);
g2d.dispose();
}catch (IOException ex) {ex.printStackTrace();}
//--------------------------1st and 2nd click point data and color
this.addMouseListener(new MouseListener() {
int[] isFristEmpty;
@Override
public void mouseClicked(MouseEvent e1) {
int[] temp =new int[3]; //external container so i can get 1st and 2nd separately
temp[0] = (int) e1.getX();
temp[1] = (int) e1.getY();
clr = blackWhite.getRGB(temp[0], temp[1]);
temp[2] = (clr & 0x00ff0000) >> 16;//--------------------bit map to find if red is there or not.
//-------------------------------------------------------since its pure b/w, if red 0, its white.
if(isFristEmpty==null) {
isFristEmpty=temp;
x[0] = temp[0]; y[0] = temp[1]; black[0]=temp[2];//------1st click
}else {
x[1] = temp[0]; y[1] = temp[1]; black[1]=temp[2];//-----2nd click
isFristEmpty=null; //so the 3rd click is considered 1st click again
flag=1;
}
if (flag==1) {
System.out.print("X1: "+x[0]+" & "+"Y1: "+y[0]+" "+"(225 if white): "+black[0]+"\t");
System.out.println("X2: "+x[1]+" & "+"Y2: "+y[1]+" "+"(225 if white): "+black[1]);
counter();
}
}
@Override public void mousePressed(MouseEvent e) {}
@Override public void mouseReleased(MouseEvent e) {}
@Override public void mouseEntered(MouseEvent e) {}
@Override public void mouseExited(MouseEvent e) {}
});
}
//--------------------------------------------DDA block
private void counter() {
int dark;
if(flag!=1) return;//-------------------to only go to counter method after it takes that 2nd click
int dx = (x[1] - x[0]);
int dy = (y[1] - y[0]);//---------makes it applicable for both inclinations (we do not have math.abs implies-> -ve goes as -ve)
step = Math.abs(dx) > Math.abs(dy) ? Math.abs(dx) : Math.abs(dy);
System.out.println("Steps: "+step);
float Xinc = dx / (float) step;//----slope change with respect to x axis
float Yinc = dy / (float) step;//----slope change with respect to y axis
tog= new int[step][3];
tog[0][0]=x[0]; tog[0][1]=y[0];
tog[0][2]= (black[0]!=0) ? 1 : 0;//------------Tertiary operator where condition is true, then while is true
//---------------------------------------------------------------send value of x1 and y1 to listOfCoordinates
float xt=x[0],yt=y[0]; int i=0, j=1;
//-------------to get all the coordinates between the 2 points1111
System.out.println(tog[0][0]+" "+tog[0][1]+" "+tog[0][2]);
while (j<step){
if(i==2) i=0;
xt += Xinc;
yt += Yinc;
tog[j][i] = (int)xt;
tog[j][i+1] = (int)yt;
j++;
}
for (i = 0; i < step; i++) {
for (j = 0; j<= 1; j++) {
System.out.print(tog[i][j]+" ");
}//****************issue zone here till...
clr = blackWhite.getRGB(tog[i][j-1], tog[i][j]);
dark = clr & 0x000000ff;//----for blue but if it is 255, its white
System.out.print(dark);
tog[i][2]= (dark!=0) ? 1 : 0;
//System.out.print(tog[i][2]);
System.out.println();
}
//********issue zone till here..
}
//------------image size and such stuff. don't touch it
@Override
public Dimension getPreferredSize() {
Dimension size = super.getPreferredSize();
if (master != null) {
size = new Dimension(master.getWidth(), master.getHeight());
}
return size;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (master != null) {
int x = (getWidth() - (master.getWidth())) / 2;
int y = (getHeight() - master.getHeight()) / 2;
g.drawImage(blackWhite, x, y, this);
}
}
}
这是所有功能良好且功能齐全的代码。希望它足够好,能有所帮助。
如果它给出 255,则该像素为白色,如果为黑色,则为 0,如果您查看已经替换为 1 和 0 的部分,那么它是白色的 1。它应该给出像素的值,但它给出了所有的白色,这就是问题所在。 x1 y1 和 x2 y2 是第一次和第二次点击的坐标。您作为输出得到的列表是这些点及其颜色之间的像素坐标列表(白色的 1 和黑色的 0)
【问题讨论】:
-
请提供minimal reproducible example。你只介绍了一个小sn-p,我们不知道所涉及的一半类型。
-
是的。我更新了代码部分,希望它看起来足够好,可以更详细
-
如果它给出 255,则该像素为白色,如果为黑色,则为 0,如果您正在查看已经替换为 1 和 0 的部分,那么它的 1 为白色。假设给出像素的值,但它给出了所有的白色,这就是问题所在。
标签: java rgb bufferedimage