array To Gray Image - Java 2D Graphics

Java examples for 2D Graphics:Image

Description

array To Gray Image

Demo Code


//package com.java2s;

import java.awt.image.BufferedImage;

import java.awt.image.WritableRaster;

import java.io.IOException;

public class Main {
    public static BufferedImage array2dToGrayImage(int[][] arrImg)
            throws IOException {
        int w = arrImg.length;
        int h = arrImg[0].length;
        int arrOut[] = new int[w * h];
        int yOffset = 0;
        for (int y = 0; y < h; y++) {
            yOffset = y * w;/*from w  w  w.ja  v a2s  .co m*/
            for (int x = 0; x < w; x++) {
                arrOut[x + yOffset] = (byte) arrImg[x][y];
            }
        }

        BufferedImage out = new BufferedImage(w, h,
                BufferedImage.TYPE_BYTE_GRAY);
        WritableRaster rOut = out.getRaster();
        rOut.setPixels(0, 0, w, h, arrOut);

        return out;
    }
}

Related Tutorials