我正在加载 BufferedImage 并根据像素的 xy 位置获取像素的 RGB 值。 在我想创建一个仅包含黑色像素的 ArrayList 之后。 我正在尝试创建列表:
List<Integer> blackpixels = new ArrayList<Integer>();
但是我在声明列表的那一行得到了这个错误:
List 类型不是通用的;它不能用参数参数化
这是我的完整代码:
import java.awt.Color;
import java.awt.List;
import java.awt.image.BufferedImage;
import java.util.*;
public class ImageTest {
public static BufferedImage Threshold(BufferedImage img) {
int height = img.getHeight();
int width = img.getWidth();
BufferedImage finalImage = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
int r = 0;
int g = 0;
int b = 0;
List<Integer> blackpixels = new ArrayList<Integer>();
for (int x = 0; x < width; x++) {
// System.out.println("Row: " + x);
try {
for (int y = 0; y < height; y++) {
//Get RGB values of pixels
int rgb = img.getRGB(x, y);
r = ImageTest.getRed(rgb);
g = ImageTest.getGreen(rgb);
b = ImageTest.getBlue(rgb);
finalImage.setRGB(x,y,ImageTest.mixColor(r, g,b));
System.out.println(r);
}
}
catch (Exception e) {
e.getMessage();
}
}
return finalImage;
}
private static int mixColor(int red, int g, int b) {
return red<<16|g<<8|b;
}
public static int getRed(int rgb) {
return (rgb & 0x00ff0000) >> 16;
}
public static int getGreen(int rgb) {
return (rgb & 0x0000ff00) >> 8;
}
public static int getBlue(int rgb) {
return (rgb & 0x000000ff) >> 0;
}
}
最佳答案
您遇到的错误是您尝试创建的 List
来自 java.awt.List
而不是 java.util.List
。如果您不打算使用 java.awt.List
,只需删除
import java.awt.List;
您已经使用 import java.util.*;
导入了正确的类,因此在删除上述句子后它应该可以正常工作。
关于java - 创建仅包含指定像素的 ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25684193/