我有JPEG的RGB图像。我想将此图像转换为像素并显示在文本文件中。怎么做?
public static int[][][] getPixelData(Image image) {
// get Image pixels in 1D int array
int width = image.getWidth(null);
int height = image.getHeight(null);
int[] imgDataOneD = imageToPixels(image);
private static int[] imageToPixels(Image image) {
int height = image.getHeight(null);
int width = image.getWidth(null);
int pixels[] = new int[width * height];
PixelGrabber grabber = new PixelGrabber(image, 0, 0, width, height, pixels, 0, width);
try {
grabber.grabPixels();
} catch (InterruptedException e) {
}
return pixels;
}
如何将此信息作为序列矢量格式存储在文本文件中?
答案 0 :(得分:0)
您可以将整数保存为以逗号分隔的列表。
确保同时保存图像宽度,以便恢复图像。
答案 1 :(得分:0)
使用以下内容。希望这会有所帮助
import java.io.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
public class ImageToText {
public static void main(String args[]) throws IOException {
File file = new File("your file path.jpg");
BufferedImage image = ImageIO.read(file);
// Getting pixel color by position x=100 and y=40
for (int i = 0; i < image.getHeight(); i++) {
for (int j = 0; j < image.getWidth(); j++) {
int color = image.getRGB(j, i);
// You can convert the colour to a readable format by changing
// to following string. It is a 6 character hex
// String clr = Integer.toHexString(color).substring(2);
// Write this int value or string value to the text file
// Add a comma or any other separator. Since it is always 6
// characters long you can avoid using comma. It will save some
// space.
}
// add a new line
}
}
}
您可以考虑自己的算法来阅读文本文件并检索数据。 : - )