Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // tested on this image http://www.goabroad.com/blog/wp-content/uploads/2012/10/GACoverPhotoContest.jpg
- int pixels[][] = convertTo2DWithoutUsingGetRGB(image);
- boolean withAlpha = false; // if you need the alpha channel change this to true
- String imgFormat = "jpg"; // if you need the alpha channel change this to png
- String imgPath = "myImage." + imgFormat;
- BufferedImage newImg = getImage(pixels, withAlpha);
- ImageIO.write(newImg, imgFormat, new File(imgPath));
- private static BufferedImage getImage(int[][] pixels, final boolean withAlpha)
- {
- // Assuming pixels was taken from convertTo2DWithoutUsingGetRGB
- // i.e. img.length == pixels.length and img.width == pixels[x].length
- BufferedImage img = new BufferedImage(pixels[0].length, pixels.length, withAlpha ? BufferedImage.TYPE_4BYTE_ABGR : BufferedImage.TYPE_3BYTE_BGR);
- for (int y = 0; y < pixels.length; y++)
- {
- for (int x = 0; x < pixels[y].length; x++)
- {
- if (withAlpha)
- img.setRGB(x, y, pixels[y][x]);
- else {
- int pixel = pixels[y][x];
- int alpha = (pixel >> 24 & 0xff);
- int red = (pixel >> 16 & 0xff);
- int green = (pixel >> 8 & 0xff);
- int blue = (pixel & 0xff);
- int rgb = (red << 16) | (green << 8) | blue;
- img.setRGB(x, y, rgb);
- }
- }
- }
- return img;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement