题
我觉得我在这里缺少一些简单的东西(像往常一样)。
我正在尝试使用Java读取PGM图像。 MATLAB做得很好 - 在MATLAB中输出图像像素(例如,一个小的32x32图像)给了我这样的东西:
1 0 11 49 94 118 118 106 95 88 85 96 124 143 142 133
但是,我的Java阅读器输出了以下内容:
1 0 11 49 94 118 118 106 95 88 85 96 124 65533 65533 65533
似乎127上方的像素值被65533填充,尽管它确实获得了一些随机值不正确,甚至几乎将整个底部行分配为-1的值。
这是我正在使用的代码:
filePath = 'imagepath.pgm'; FileInputStream fileInputStream = new FileInputStream(filePath); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream)); // read the header information ... int [][] data2D = new int [picWidth] [picHeight]; for (int row = 0; row < picHeight; row++) { for (int col = 0; col < picWidth; col++) { data2D[row][col] = bufferedReader.read(); System.out.print(data2D[row][col] + " "); } System.out.println(); } fileInputStream.close();
任何想法都将不胜感激。
编辑 这是未签名的PGM值:
1 0 11 49 94 118 118 106 95 88 85 96 124 143 142 133 30 26 29 57 96 122 125 114 102 94 91 101 127 146 145 136 96 85 70 75 101 128 136 126 111 106 106 112 131 149 153 147 163 147 114 93 99 120 132 123 110 113 124 129 137 154 166 168 215 195 149 105 88 99 114 111 106 123 148 158 160 174 191 197 245 224 173 115 81 82 100 109 117 144 179 194 194 205 222 230 235 217 170 115 78 78 113 117 100 83 80 212 214 226 244 253 178 167 135 93 68 78 123 129 106 77 69 202 204 222 244 255 114 110 92 64 54 81 107 105 83 59 56 182 184 201 222 231 79 80 71 52 55 97 67 55 41 33 42 184 179 181 185 183 62 66 65 52 63 115 29 16 12 17 30 209 197 174 150 132 40 47 52 44 55 109 171 196 188 186 208 229 218 179 136 107 31 38 44 37 43 89 145 167 158 159 191 223 219 179 133 105 48 52 56 51 57 91 128 133 117 120 157 196 200 168 128 105 64 67 70 73 87 114 127 107 79 81 118 159 173 154 123 104 63 67 73 83 107 132 129 91 54 54 88 130 153 146 123 106
标题看起来像这样:
P5 # MatLab PGMWRITE file, saved 27-Jun-2002 16 16 255
编辑#2
这是以下概念代码证明的完整输出:
Skipping unknow token: "" Skipping unknow token: "1^vvj_XU`|���" Skipping unknow token: "" Skipping unknow token: "9`z}rf^[e���`UFKe��~ojjp������r]cx�{nq|������ÕiXcroj{��������sQRdmu��������٪sNNqudSP�����]DN{�jME�����rn\@6QkiS;8�����OPG47aC7)!*�����>BA4?s" Skipping unknow token: "" Skipping unknow token: "" Skipping unknow token: "�Ů��(/4,7m�ļ���ڳ�k" Skipping unknow token: "&,%+Y������۳�i04839[��ux��Ȩ�i@CFIWrkOQv���{h?CISk��[66X���{j" Exception in thread "main" java.util.NoSuchElementException at java.util.Scanner.throwFor(Scanner.java:838) at java.util.Scanner.next(Scanner.java:1347) at Test.main(Test.java:49)
抛出中提到的第49行是:
System.out.println(String.format("Skipping unknow token: \"%s\"", scan.next()));
我敢肯定,这个问题与这些图像文件既由ASCII文本/数字以及二进制图像数据组成。但是,如果Java阅读PNG没有问题,为什么缺乏对PGM的支持?
编辑3
好的,我找到了一个有效的实现……不幸的是,它已被弃用:
filePath = "imagepath.pgm"
FileInputStream fileInputStream = new FileInputStream(filePath);
DataInputStream dis = new DataInputStream(fileInputStream);
StreamTokenizer streamTokenizer = new StreamTokenizer(dis);
// read header text using StreamTokenizer.nextToken()
data2D = new int [picWidth] [picHeight];
for (int row = 0; row < picHeight; row++) {
for (int col = 0; col < picWidth; col++) {
data2D[row][col] = dis.readUnsignedByte();
System.out.print(data2D[row][col] + " ");
}
System.out.println();
}
根据Java文档, StreamTokenizer(InputStream)
构造函数被弃用,因为 DataInputStream.readLine()
方法不能正确地将原始字节转换为字符。但是,在标题上的这种特定情况下,它似乎有效,显然适用于随后的二进制图像数据。
不幸的是,它仍然被弃用,似乎通过互混合 BufferedReader
正如文档所暗示的那样仅导致 EOFException
在阅读标头后尝试使用 DataInputStream
读取原始字节。仍在寻找解决方案...
解决方案
代码的问题在于,您正在使用错误的类来读取文件的原始数据。作为 BufferedReader
文档说:
public int read() throws IOException
读一个字符。
返回:字符读取,作为范围0到65535(0x00-0xffff)的整数,或-1如果到达了流的末端
所以每个电话 read()
的方法 BufferedReader
实际上,从输入流中消耗一个或两个字节(基于字符编码),这不是您想要的。这也解释了为什么您得到很多-1:流的结束比您想象的要早得多。
由于PGM包含ASCII小数的值,因此很容易使用 扫描器 班级。
这是一个 几乎未经测试 表明如何读取如何读取PGM图像的代码:
- 它包含魔术数之后的单个评论(即它没有以#开头的行,除第二个以外的线)
- PGM文件正好4行长。
这是代码:
String filePath = "image.pgm";
fileInputStream = new FileInputStream(filePath);
Scanner scan = new Scanner(fileInputStream);
// Discard the magic number
scan.nextLine();
// Discard the comment line
scan.nextLine();
// Read pic width, height and max value
int picWidth = scan.nextInt();
int picHeight = scan.nextInt();
int maxvalue = scan.nextInt();
fileInputStream.close();
// Now parse the file as binary data
fileInputStream = new FileInputStream(filePath);
DataInputStream dis = new DataInputStream(fileInputStream);
// look for 4 lines (i.e.: the header) and discard them
int numnewlines = 4;
while (numnewlines > 0) {
char c;
do {
c = (char)(dis.readUnsignedByte());
} while (c != '\n');
numnewlines--;
}
// read the image data
int[][] data2D = new int[picHeight][picWidth];
for (int row = 0; row < picHeight; row++) {
for (int col = 0; col < picWidth; col++) {
data2D[row][col] = dis.readUnsignedByte();
System.out.print(data2D[row][col] + " ");
}
System.out.println();
}
需要实施:对评论行的支持,每个元素的值应除以 maxvalue
, ,错误检查错误的文件,异常处理。我使用UNIX端线在PGM文件上对其进行了测试,但它也应该在Windows上工作。
让我强调 这不是PGM解析器的强大或完整实施. 。此代码旨在作为概念证明,可能可以满足您的需求。
如果您确实需要强大的PGM解析器,则可以使用由 NETPBM.