Java 从像素阵列转换图像 我正在尝试将像素矩阵转换为图像,但它不起作用。有人能帮我吗?下面是代码。在这里,我添加了一个print语句来检查它是否正在运行,但它也没有执行。有人能帮我吗 这是给出问题的代码 这是完整的代码

Java 从像素阵列转换图像 我正在尝试将像素矩阵转换为图像,但它不起作用。有人能帮我吗?下面是代码。在这里,我添加了一个print语句来检查它是否正在运行,但它也没有执行。有人能帮我吗 这是给出问题的代码 这是完整的代码,java,image-processing,Java,Image Processing,@不知道多少,但更好的是,我已经在这里粘贴了完整的代码。输出不会出现,也就是说,它不会在任何地方创建名为out.png的文件。您的主要目标是什么?从文件创建图像?是的,我想尝试将像素数组转换为图像并在@Nikolayy处写入它。您不需要将文件转换为像素数组,然后再转换为图像-只需执行以下操作:Image Image=ImageIO.read(新文件(args[0])@Nikolay我知道我可以做到这一点,但这是双三次插值的一部分,我想在没有任何第三方库的情况下为双三次插值编写一个代码,所以通过编


@不知道多少,但更好的是,我已经在这里粘贴了完整的代码。输出不会出现,也就是说,它不会在任何地方创建名为out.png的文件。您的主要目标是什么?从文件创建图像?是的,我想尝试将像素数组转换为图像并在@Nikolayy处写入它。您不需要将文件转换为像素数组,然后再转换为图像-只需执行以下操作:
Image Image=ImageIO.read(新文件(args[0])@Nikolay我知道我可以做到这一点,但这是双三次插值的一部分,我想在没有任何第三方库的情况下为双三次插值编写一个代码,所以通过编写此代码进行测试,是否可以将矩阵转换为图像。在将图像转换为矩阵后,我将对该矩阵应用双三次运算,然后将得到的矩阵转换为图像。
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.awt.image.PixelGrabber;
import java.awt.image.WritableRaster;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public final class Util {
    /**
     * Converts a java.awt.Image into an array of pixels
     */
    public static int[] convertToPixels(Image img) {
        int width = img.getWidth(null);

        int height = img.getHeight(null);
        int[] pixel = new int[width * height];

        PixelGrabber pg = new PixelGrabber(img, 0, 0, width, height, pixel, 0, width);
        try {
            pg.grabPixels();
        } catch (InterruptedException e) {
            throw new IllegalStateException("Error: Interrupted Waiting for Pixels");
        }
        if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
            throw new IllegalStateException("Error: Image Fetch Aborted");
        }
        return pixel;
    }

    public static Image getImageFromArray(int[] pixels, int width, int height) throws IOException {
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        WritableRaster raster = (WritableRaster) image.getData();
        raster.setPixels(0, 0, width, height, pixels);
        File output = new File("C:\\out.png");
        ImageIO.write(image, "png", output);
        System.out.print("written");
        return image;
    }

    public static void main(String args[]) throws IOException {
        int width, height;
        BufferedImage source = ImageIO.read(new File(args[0]));
        width = source.getWidth();
        height = source.getHeight();
        // Util obj = new Util();
        Util.getImageFromArray(convertToPixels(source), width, height);
    }
}
public static Image getImageFromArray(int[] pixels, int width, int height) throws IOException {
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    WritableRaster raster = image.getRaster(); //faster - no copy
    raster.setDataElements(0, 0, width, height, pixels); //instead of setPixels
    File output = new File("C:\\out.png");
    ImageIO.write(image, "png", output);
    System.out.print("written");
    return image;
}