Here you can find the source of intensityArrayToBufferedImage(byte[] array, int w, int h)
Parameter | Description |
---|---|
array | The array to convert |
w | the width of the resulting image |
h | The height of the resulting image |
public static BufferedImage intensityArrayToBufferedImage(byte[] array, int w, int h)
//package com.java2s; /******************************************************************************* * + -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- + * | | * faint - The Face Annotation Interface * | Copyright (C) 2007 Malte Mathiszig | * // ww w . j a va 2 s . c o m * | This program is free software: you can redistribute it and/or modify | * it under the terms of the GNU General Public License as published by * | the Free Software Foundation, either version 3 of the License, or | * (at your option) any later version. * | | * This program is distributed in the hope that it will be useful, * | but WITHOUT ANY WARRANTY; without even the implied warranty of | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | GNU General Public License for more details. | * * | You should have received a copy of the GNU General Public License | * along with this program. If not, see <http://www.gnu.org/licenses/>. * | | * + -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- + *******************************************************************************/ import java.awt.Dimension; import java.awt.Transparency; import java.awt.color.ColorSpace; import java.awt.image.BufferedImage; import java.awt.image.ColorModel; import java.awt.image.ComponentColorModel; import java.awt.image.DataBuffer; import java.awt.image.DataBufferByte; import java.awt.image.Raster; import java.awt.image.SampleModel; import java.awt.image.WritableRaster; public class Main { /** * Converts the given intensity array into a grayscale image of w x h dimensions. * * @param array The array to convert * @param w the width of the resulting image * @param h The height of the resulting image * @return The grayscale image that the intensity values represent */ public static BufferedImage intensityArrayToBufferedImage(byte[] array, int w, int h) { ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY); int[] nBits = { 8 }; ColorModel cm = new ComponentColorModel(cs, nBits, false, true, Transparency.OPAQUE, DataBuffer.TYPE_BYTE); SampleModel sm = cm.createCompatibleSampleModel(w, h); DataBufferByte db = new DataBufferByte(array, w * h); WritableRaster raster = Raster.createWritableRaster(sm, db, null); BufferedImage bm = new BufferedImage(cm, raster, false, null); return bm; } /** * Convert the given intensity array to a buffered image of {@code size} dimensions. * * @param averageFace The intensity of the pixels * @param face_thumbnail_size The size of the image. * @return The image. */ public static BufferedImage intensityArrayToBufferedImage(byte[] averageFace, Dimension size) { return intensityArrayToBufferedImage(averageFace, size.width, size.height); } }