Java BufferedImage from Byte Array fromByteIntensity(BufferedImage image, byte[] bytes)

Here you can find the source of fromByteIntensity(BufferedImage image, byte[] bytes)

Description

from Byte Intensity

License

Apache License

Declaration

public static BufferedImage fromByteIntensity(BufferedImage image, byte[] bytes) 

Method Source Code

//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);
    }
}

Related

  1. decodeForRGBBufferedImage(String filePath)
  2. fromByteArray(byte[] byteArray)
  3. fromByteArray(byte[] bytes)
  4. fromByteArray(byte[] data)
  5. fromByteArray(byte[] imagebytes)
  6. fromIntIntensityScaled(int width, int height, int[] data)
  7. fromIntRGB(BufferedImage image, int[] rgb)
  8. getBufferedImage(byte[] imageBytes)
  9. getImage(byte[] bytes)