001package com.github.sarxos.webcam.util;
002
003import java.awt.image.BufferedImage;
004import java.awt.image.DataBuffer;
005import java.awt.image.DataBufferByte;
006import java.io.ByteArrayOutputStream;
007import java.io.IOException;
008
009import javax.imageio.ImageIO;
010
011import com.github.sarxos.webcam.WebcamException;
012
013
014public class ImageUtils {
015
016        /**
017         * Graphics Interchange Format.
018         */
019        public static final String FORMAT_GIF = "GIF";
020
021        /**
022         * Portable Network Graphic format.
023         */
024        public static final String FORMAT_PNG = "PNG";
025
026        /**
027         * Joint Photographic Experts Group format.
028         */
029        public static final String FORMAT_JPG = "JPG";
030
031        /**
032         * Bitmap image format.
033         */
034        public static final String FORMAT_BMP = "BMP";
035
036        /**
037         * Wireless Application Protocol Bitmap image format.
038         */
039        public static final String FORMAT_WBMP = "WBMP";
040
041        /**
042         * Convert {@link BufferedImage} to byte array.
043         * 
044         * @param image the image to be converted
045         * @param format the output image format
046         * @return New array of bytes
047         */
048        public static byte[] toByteArray(BufferedImage image, String format) {
049
050                byte[] bytes = null;
051                ByteArrayOutputStream baos = new ByteArrayOutputStream();
052
053                try {
054                        ImageIO.write(image, format, baos);
055                        bytes = baos.toByteArray();
056                } catch (IOException e) {
057                        throw new WebcamException(e);
058                } finally {
059                        try {
060                                baos.close();
061                        } catch (IOException e) {
062                                throw new WebcamException(e);
063                        }
064                }
065
066                return bytes;
067        }
068
069        public static byte[] toRawByteArray(BufferedImage image) {
070
071                DataBuffer dbuf = image.getRaster().getDataBuffer();
072
073                if (dbuf instanceof DataBufferByte) {
074
075                        return ((DataBufferByte) dbuf).getData();
076
077                } else {
078
079                        int w = image.getWidth();
080                        int h = image.getHeight();
081                        int n = w * h;
082
083                        byte[] bytes = new byte[n * 3];
084
085                        int i, x, y, rgb;
086
087                        for (i = 0; i < n; i++) {
088
089                                x = i % w;
090                                y = i / h;
091
092                                rgb = image.getRGB(x, y);
093
094                                bytes[i * 3 + 0] = (byte) ((rgb >> 16) & 0xff);
095                                bytes[i * 3 + 1] = (byte) ((rgb >> 8) & 0xff);
096                                bytes[i * 3 + 2] = (byte) (rgb & 0xff);
097                        }
098
099                        return bytes;
100                }
101        }
102}