Display image from 2D array data in java

I have some data that is loaded from a text file corresponding to my image file. this data is now in a 2D array. I want to show this image. apparently, the image will be in bufferedimage format. but my just plain two-dimensional format. You can also resize the image, which means it will be twice as large (interpolation between the course is required)

In other words, how can we make "imshow" and "imresize" the matlab equivalent in java?

+4
source share
2 answers

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); // Or _BICUBIC g2d.scale(2, 2); g2d.drawImage(img, 0, 0, this); } }; panel.setPreferredSize(new Dimension(WIDTH*2, HEIGHT*2)); frame.getContentPane().add(panel); frame.pack(); frame.setVisible(true); } } 

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.

+3
source

Take a look at MemoryImageSource . It may not do what you need (since Java tends to use int / byte for image data), but it will at least send you along the correct route.

http://www.javaworld.com/javatips/jw-javatip16.html

+1
source

All Articles