在 Java 中将二维整数数组显示为图像 [英] Displaying a 2D array of integers as an image in Java

查看:32
本文介绍了在 Java 中将二维整数数组显示为图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用 Java 创建一个应用程序,该应用程序将基于整数数组打印图像,其中每个整数代表一种颜色.有没有直接的方法来实现这一点?

I'm trying to create an application in Java that will print an image based on an array of integers where each integer represents a color. Is there a straightforward way to accomplish this?

public void displayImage(int[][] arr){
    for(int i = 0; i < arr.length; i++){
        for(int j = 0; j < arr[0].length; j++){
            switch(arr[i][j]){
                case 1:
                //print a gray pixel at (i, j) within a frame
                case 0:
                //print a green pixel at (i, j) within a frame
                case 2:
                //print a white pixel at (i, j) within a frame
            }
        }
    }
}

推荐答案

您可以使用 BufferedImage,如下面的 示例.

附录:引用的示例更新了图像的底层 WritableRaster 使用 int 颜色分量数组,但 setRGB() 很方便,如果颜色是已经可用.

Addendum: The example cited updates the image's underlying WritableRaster using an int array of color components, but setRGB() is convenient if the color is already available.

这篇关于在 Java 中将二维整数数组显示为图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆