Java BufferedImage Pixel pixelsToImage(int[][] pixels)

Here you can find the source of pixelsToImage(int[][] pixels)

Description

pixels To Image

License

Apache License

Declaration

public static BufferedImage pixelsToImage(int[][] pixels) 

Method Source Code

//package com.java2s;
/*/*w  ww  .  j av  a  2  s . c  om*/
 * Copyright 2009-2012 Michael Tamm
 *
 * 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 pixelsToImage(int[][] pixels) {
        if (pixels == null) {
            return null;
        }
        int w = pixels.length;
        if (w > 0) {
            int h = pixels[0].length;
            if (h > 0) {
                BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
                for (int x = 0; x < w; ++x) {
                    int[] column = pixels[x];
                    for (int y = 0; y < h; ++y) {
                        image.setRGB(x, y, column[y]);
                    }
                }
                return image;
            } else {
                throw new IllegalArgumentException("pixels[0].length must not be 0.");
            }
        } else {
            throw new IllegalArgumentException("pixels.length must not be 0.");
        }
    }

    public static BufferedImage pixelsToImage(boolean[][] pixels) {
        if (pixels == null) {
            return null;
        }
        int w = pixels.length;
        if (w > 0) {
            int h = pixels[0].length;
            if (h > 0) {
                BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
                for (int x = 0; x < w; ++x) {
                    boolean[] column = pixels[x];
                    for (int y = 0; y < h; ++y) {
                        image.setRGB(x, y, column[y] ? 0 : 0xFFFFFF);
                    }
                }
                return image;
            } else {
                throw new IllegalArgumentException("pixels[0].length must not be 0.");
            }
        } else {
            throw new IllegalArgumentException("pixels.length must not be 0.");
        }
    }
}

Related

  1. imageFromIntArray(int[] pixels, int w, int h)
  2. pixels(BufferedImage image)
  3. pixelsb(BufferedImage image)
  4. pixelsFromBufferedImage(final BufferedImage image)
  5. pixelsg(BufferedImage image)
  6. setRGB(BufferedImage image, int x, int y, int width, int height, int[] pixels)
  7. toRgbPixels(BufferedImage image)