Here you can find the source of fromByteIntensity(BufferedImage image, byte[] bytes)
public static BufferedImage fromByteIntensity(BufferedImage image, byte[] bytes)
//package com.java2s; /*//from w w w . jav a 2 s. co m * Copyright 2005 Tom Gibara * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ import java.awt.image.BufferedImage; public class Main { public static BufferedImage fromByteIntensity(BufferedImage image, byte[] bytes) { int width = image.getWidth(); int height = image.getHeight(); switch (image.getType()) { case BufferedImage.TYPE_BYTE_BINARY: case BufferedImage.TYPE_BYTE_GRAY: { image.getWritableTile(0, 0).setDataElements(0, 0, width, height, bytes); return image; } case BufferedImage.TYPE_INT_ARGB: case BufferedImage.TYPE_INT_ARGB_PRE: case BufferedImage.TYPE_INT_RGB: { int[] data = new int[width * height]; for (int i = 0; i < bytes.length; i++) { int b = bytes[i] & 0xff; data[i] = (((((0xff << 8) | b) << 8) | b) << 8) | b; } image.getWritableTile(0, 0).setDataElements(0, 0, width, height, data); return image; } case BufferedImage.TYPE_3BYTE_BGR: { byte[] data = new byte[width * height * 3]; int offset = 0; for (int i = 0; i < bytes.length; i++) { byte b = bytes[i]; data[offset++] = b; data[offset++] = b; data[offset++] = b; } image.getWritableTile(0, 0).setDataElements(0, 0, width, height, data); return image; } default: throw new IllegalArgumentException("Unsupported image type: " + image.getType()); } } public static BufferedImage fromByteIntensity(int width, int height, int imageType, byte[] bytes) { BufferedImage image = new BufferedImage(width, height, imageType); return fromByteIntensity(image, bytes); } }