There is no simple conversion method between an array-based intensity matrix and the rendered image in Java, at least not what I know about. There is also no simple single line method for displaying an image on a screen, etc.
However, it is correct that BufferedImage will be a viable solution in this case. What you need to do is create a BufferedImage of the right size, and then skip your 2D intensity matrix and fill in the colors in the resulting image.
Once you have the data in the BufferedImage form, you can use it directly for rendering. For example, you can create a JFrame with a custom JPanel component to display an image. The following code example illustrates this procedure: (Note that this assumes that your image data in a 2D array is scaled to fit the interval [0,1]. If this is not the case, it will need to be scaled again to fill in BufferedImage .)
public class ImageTest { private static final int HEIGHT = 250; private static final int WIDTH = 250; public static void main(String[] args) { double[][] data = new double[WIDTH][HEIGHT]; Random r = new Random(); for(int i = 0; i < WIDTH; i++) { for(int j = 0; j < HEIGHT; j++) { data[i][j] = r.nextDouble(); } } final BufferedImage img = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); Graphics2D g = (Graphics2D)img.getGraphics(); for(int i = 0; i < WIDTH; i++) { for(int j = 0; j < HEIGHT; j++) { float c = (float) data[i][j]; g.setColor(new Color(c, c, c)); g.fillRect(i, j, 1, 1); data[i][j] = r.nextDouble(); } } JFrame frame = new JFrame("Image test"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel() { @Override protected void paintComponent(Graphics g) { Graphics2D g2d = (Graphics2D)g; g2d.clearRect(0, 0, getWidth(), getHeight()); g2d.setRenderingHint( RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
If you are happy with the resizing and interpolation in the resulting output image, you can achieve this by simply scaling the graphics context and including the interpolation render hint on it, as shown above, when displaying / rendering the image. (This, of course, can be done in BufferedImage in a similar way.
source share