Search in sources :

Example 96 with BufferedImage

use of java.awt.image.BufferedImage in project jfinal by jfinal.

the class CaptchaRender method render.

/**
	 * 生成验证码
	 */
public void render() {
    Captcha captcha = createCaptcha();
    CaptchaManager.me().getCaptchaCache().put(captcha);
    Cookie cookie = new Cookie(captchaName, captcha.getKey());
    cookie.setMaxAge(-1);
    cookie.setPath("/");
    response.addCookie(cookie);
    response.setHeader("Pragma", "no-cache");
    response.setHeader("Cache-Control", "no-cache");
    response.setDateHeader("Expires", 0);
    response.setContentType("image/jpeg");
    ServletOutputStream sos = null;
    try {
        BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
        drawGraphic(captcha.getValue(), image);
        sos = response.getOutputStream();
        ImageIO.write(image, "jpeg", sos);
    } catch (IOException e) {
        if (getDevMode()) {
            throw new RenderException(e);
        }
    } catch (Exception e) {
        throw new RenderException(e);
    } finally {
        if (sos != null) {
            try {
                sos.close();
            } catch (IOException e) {
                LogKit.logNothing(e);
            }
        }
    }
}
Also used : Cookie(javax.servlet.http.Cookie) RenderException(com.jfinal.render.RenderException) ServletOutputStream(javax.servlet.ServletOutputStream) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage) IOException(java.io.IOException) RenderException(com.jfinal.render.RenderException)

Example 97 with BufferedImage

use of java.awt.image.BufferedImage in project Smack by igniterealtime.

the class WholeImageFilter method filter.

@Override
public BufferedImage filter(BufferedImage src, BufferedImage dst) {
    int width = src.getWidth();
    int height = src.getHeight();
    int type = src.getType();
    WritableRaster srcRaster = src.getRaster();
    originalSpace = new Rectangle(0, 0, width, height);
    transformedSpace = new Rectangle(0, 0, width, height);
    transformSpace(transformedSpace);
    if (dst == null) {
        ColorModel dstCM = src.getColorModel();
        dst = new BufferedImage(dstCM, dstCM.createCompatibleWritableRaster(transformedSpace.width, transformedSpace.height), dstCM.isAlphaPremultiplied(), null);
    }
    WritableRaster dstRaster = dst.getRaster();
    int[] inPixels = getRGB(src, 0, 0, width, height, null);
    inPixels = filterPixels(width, height, inPixels, transformedSpace);
    setRGB(dst, 0, 0, transformedSpace.width, transformedSpace.height, inPixels);
    return dst;
}
Also used : WritableRaster(java.awt.image.WritableRaster) ColorModel(java.awt.image.ColorModel) Rectangle(java.awt.Rectangle) BufferedImage(java.awt.image.BufferedImage)

Example 98 with BufferedImage

use of java.awt.image.BufferedImage in project Apktool by iBotPeaches.

the class BuildAndDecodeTest method ninePatchImageColorTest.

@Test
public void ninePatchImageColorTest() throws BrutException, IOException {
    char slash = File.separatorChar;
    String location = slash + "res" + slash + "drawable-xhdpi" + slash;
    File control = new File((sTestOrigDir + location), "9patch.9.png");
    File test = new File((sTestNewDir + location), "9patch.9.png");
    BufferedImage controlImage = ImageIO.read(control);
    BufferedImage testImage = ImageIO.read(test);
    // lets start with 0,0 - empty
    assertEquals(controlImage.getRGB(0, 0), testImage.getRGB(0, 0));
    // then with 30, 0 - black
    assertEquals(controlImage.getRGB(30, 0), testImage.getRGB(30, 0));
    // then 30, 30 - blue
    assertEquals(controlImage.getRGB(30, 30), testImage.getRGB(30, 30));
}
Also used : ExtFile(brut.directory.ExtFile) BufferedImage(java.awt.image.BufferedImage)

Example 99 with BufferedImage

use of java.awt.image.BufferedImage in project jmonkeyengine by jMonkeyEngine.

the class CursorLoader method parseICOImage.

private BufferedImage[] parseICOImage(byte[] icoimage) throws IOException {
    /*
         * Most of this is original code by Jeff Friesen at
         * http://www.informit.com/articles/article.aspx?p=1186882&seqNum=3
         */
    BufferedImage[] bi;
    // Check resource type field.
    // directory entry length
    int DE_LENGTH = 16;
    // BITMAPINFOHEADER length
    int BMIH_LENGTH = 40;
    if (icoimage[2] != 1 && icoimage[2] != 2 || icoimage[3] != 0) {
        throw new IllegalArgumentException("Bad data in ICO/CUR file. ImageType has to be either 1 or 2.");
    }
    int numImages = ubyte(icoimage[5]);
    numImages <<= 8;
    numImages |= icoimage[4];
    bi = new BufferedImage[numImages];
    int[] colorCount = new int[numImages];
    for (int i = 0; i < numImages; i++) {
        int width = ubyte(icoimage[FDE_OFFSET + i * DE_LENGTH]);
        int height = ubyte(icoimage[FDE_OFFSET + i * DE_LENGTH + 1]);
        colorCount[i] = ubyte(icoimage[FDE_OFFSET + i * DE_LENGTH + 2]);
        int bytesInRes = ubyte(icoimage[FDE_OFFSET + i * DE_LENGTH + 11]);
        bytesInRes <<= 8;
        bytesInRes |= ubyte(icoimage[FDE_OFFSET + i * DE_LENGTH + 10]);
        bytesInRes <<= 8;
        bytesInRes |= ubyte(icoimage[FDE_OFFSET + i * DE_LENGTH + 9]);
        bytesInRes <<= 8;
        bytesInRes |= ubyte(icoimage[FDE_OFFSET + i * DE_LENGTH + 8]);
        int imageOffset = ubyte(icoimage[FDE_OFFSET + i * DE_LENGTH + 15]);
        imageOffset <<= 8;
        imageOffset |= ubyte(icoimage[FDE_OFFSET + i * DE_LENGTH + 14]);
        imageOffset <<= 8;
        imageOffset |= ubyte(icoimage[FDE_OFFSET + i * DE_LENGTH + 13]);
        imageOffset <<= 8;
        imageOffset |= ubyte(icoimage[FDE_OFFSET + i * DE_LENGTH + 12]);
        if (icoimage[imageOffset] == 40 && icoimage[imageOffset + 1] == 0 && icoimage[imageOffset + 2] == 0 && icoimage[imageOffset + 3] == 0) {
            // BITMAPINFOHEADER detected
            int _width = ubyte(icoimage[imageOffset + 7]);
            _width <<= 8;
            _width |= ubyte(icoimage[imageOffset + 6]);
            _width <<= 8;
            _width |= ubyte(icoimage[imageOffset + 5]);
            _width <<= 8;
            _width |= ubyte(icoimage[imageOffset + 4]);
            if (width == 0) {
                width = _width;
            }
            int _height = ubyte(icoimage[imageOffset + 11]);
            _height <<= 8;
            _height |= ubyte(icoimage[imageOffset + 10]);
            _height <<= 8;
            _height |= ubyte(icoimage[imageOffset + 9]);
            _height <<= 8;
            _height |= ubyte(icoimage[imageOffset + 8]);
            if (height == 0) {
                // Divide by 2.
                height = _height >> 1;
            }
            int planes = ubyte(icoimage[imageOffset + 13]);
            planes <<= 8;
            planes |= ubyte(icoimage[imageOffset + 12]);
            int bitCount = ubyte(icoimage[imageOffset + 15]);
            bitCount <<= 8;
            bitCount |= ubyte(icoimage[imageOffset + 14]);
            if (colorCount[i] == 0) {
                if (planes == 1) {
                    if (bitCount == 1) {
                        colorCount[i] = 2;
                    } else if (bitCount == 4) {
                        colorCount[i] = 16;
                    } else if (bitCount == 8) {
                        colorCount[i] = 256;
                    } else if (bitCount != 32) {
                        colorCount[i] = (int) Math.pow(2, bitCount);
                    }
                } else {
                    colorCount[i] = (int) Math.pow(2, bitCount * planes);
                }
            }
            bi[i] = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
            // Parse image to image buffer.
            int colorTableOffset = imageOffset + BMIH_LENGTH;
            if (colorCount[i] == 2) {
                int xorImageOffset = colorTableOffset + 2 * 4;
                int scanlineBytes = calcScanlineBytes(width, 1);
                int andImageOffset = xorImageOffset + scanlineBytes * height;
                int[] masks = { 128, 64, 32, 16, 8, 4, 2, 1 };
                for (int row = 0; row < height; row++) {
                    for (int col = 0; col < width; col++) {
                        int index;
                        if ((ubyte(icoimage[xorImageOffset + row * scanlineBytes + col / 8]) & masks[col % 8]) != 0) {
                            index = 1;
                        } else {
                            index = 0;
                        }
                        int rgb = 0;
                        rgb |= (ubyte(icoimage[colorTableOffset + index * 4 + 2]));
                        rgb <<= 8;
                        rgb |= (ubyte(icoimage[colorTableOffset + index * 4 + 1]));
                        rgb <<= 8;
                        rgb |= (ubyte(icoimage[colorTableOffset + index * 4]));
                        if ((ubyte(icoimage[andImageOffset + row * scanlineBytes + col / 8]) & masks[col % 8]) != 0) {
                            bi[i].setRGB(col, height - 1 - row, rgb);
                        } else {
                            bi[i].setRGB(col, height - 1 - row, 0xff000000 | rgb);
                        }
                    }
                }
            } else if (colorCount[i] == 16) {
                int xorImageOffset = colorTableOffset + 16 * 4;
                int scanlineBytes = calcScanlineBytes(width, 4);
                int andImageOffset = xorImageOffset + scanlineBytes * height;
                int[] masks = { 128, 64, 32, 16, 8, 4, 2, 1 };
                for (int row = 0; row < height; row++) {
                    for (int col = 0; col < width; col++) {
                        int index;
                        if (// even
                        (col & 1) == 0) {
                            index = ubyte(icoimage[xorImageOffset + row * scanlineBytes + col / 2]);
                            index >>= 4;
                        } else {
                            index = ubyte(icoimage[xorImageOffset + row * scanlineBytes + col / 2]) & 15;
                        }
                        int rgb = 0;
                        rgb |= (ubyte(icoimage[colorTableOffset + index * 4 + 2]));
                        rgb <<= 8;
                        rgb |= (ubyte(icoimage[colorTableOffset + index * 4 + 1]));
                        rgb <<= 8;
                        rgb |= (ubyte(icoimage[colorTableOffset + index * 4]));
                        if ((ubyte(icoimage[andImageOffset + row * calcScanlineBytes(width, 1) + col / 8]) & masks[col % 8]) != 0) {
                            bi[i].setRGB(col, height - 1 - row, rgb);
                        } else {
                            bi[i].setRGB(col, height - 1 - row, 0xff000000 | rgb);
                        }
                    }
                }
            } else if (colorCount[i] == 256) {
                int xorImageOffset = colorTableOffset + 256 * 4;
                int scanlineBytes = calcScanlineBytes(width, 8);
                int andImageOffset = xorImageOffset + scanlineBytes * height;
                int[] masks = { 128, 64, 32, 16, 8, 4, 2, 1 };
                for (int row = 0; row < height; row++) {
                    for (int col = 0; col < width; col++) {
                        int index;
                        index = ubyte(icoimage[xorImageOffset + row * scanlineBytes + col]);
                        int rgb = 0;
                        rgb |= (ubyte(icoimage[colorTableOffset + index * 4 + 2]));
                        rgb <<= 8;
                        rgb |= (ubyte(icoimage[colorTableOffset + index * 4 + 1]));
                        rgb <<= 8;
                        rgb |= (ubyte(icoimage[colorTableOffset + index * 4]));
                        if ((ubyte(icoimage[andImageOffset + row * calcScanlineBytes(width, 1) + col / 8]) & masks[col % 8]) != 0) {
                            bi[i].setRGB(col, height - 1 - row, rgb);
                        } else {
                            bi[i].setRGB(col, height - 1 - row, 0xff000000 | rgb);
                        }
                    }
                }
            } else if (colorCount[i] == 0) {
                int scanlineBytes = calcScanlineBytes(width, 32);
                for (int row = 0; row < height; row++) {
                    for (int col = 0; col < width; col++) {
                        int rgb = ubyte(icoimage[colorTableOffset + row * scanlineBytes + col * 4 + 3]);
                        rgb <<= 8;
                        rgb |= ubyte(icoimage[colorTableOffset + row * scanlineBytes + col * 4 + 2]);
                        rgb <<= 8;
                        rgb |= ubyte(icoimage[colorTableOffset + row * scanlineBytes + col * 4 + 1]);
                        rgb <<= 8;
                        rgb |= ubyte(icoimage[colorTableOffset + row * scanlineBytes + col * 4]);
                        bi[i].setRGB(col, height - 1 - row, rgb);
                    }
                }
            }
        } else if (ubyte(icoimage[imageOffset]) == 0x89 && icoimage[imageOffset + 1] == 0x50 && icoimage[imageOffset + 2] == 0x4e && icoimage[imageOffset + 3] == 0x47 && icoimage[imageOffset + 4] == 0x0d && icoimage[imageOffset + 5] == 0x0a && icoimage[imageOffset + 6] == 0x1a && icoimage[imageOffset + 7] == 0x0a) {
            // PNG detected
            ByteArrayInputStream bais;
            bais = new ByteArrayInputStream(icoimage, imageOffset, bytesInRes);
            bi[i] = ImageIO.read(bais);
        } else {
            throw new IllegalArgumentException("Bad data in ICO/CUR file. BITMAPINFOHEADER or PNG " + "expected");
        }
    }
    // This array can now be garbage collected.
    icoimage = null;
    return bi;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) BufferedImage(java.awt.image.BufferedImage)

Example 100 with BufferedImage

use of java.awt.image.BufferedImage in project jmonkeyengine by jMonkeyEngine.

the class CombinedTexture method scale.

/**
     * This method scales the given texture to the given size.
     * 
     * @param texture
     *            the texture to be scaled
     * @param width
     *            new width of the texture
     * @param height
     *            new height of the texture
     */
private void scale(Texture2D texture, int width, int height) {
    // first determine if scaling is required
    boolean scaleRequired = texture.getImage().getWidth() != width || texture.getImage().getHeight() != height;
    if (scaleRequired) {
        Image image = texture.getImage();
        BufferedImage sourceImage = ImageToAwt.convert(image, false, true, 0);
        int sourceWidth = sourceImage.getWidth();
        int sourceHeight = sourceImage.getHeight();
        BufferedImage targetImage = new BufferedImage(width, height, sourceImage.getType());
        Graphics2D g = targetImage.createGraphics();
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g.drawImage(sourceImage, 0, 0, width, height, 0, 0, sourceWidth, sourceHeight, null);
        g.dispose();
        Image output = new ImageLoader().load(targetImage, false);
        image.setWidth(width);
        image.setHeight(height);
        image.setData(output.getData(0));
        image.setFormat(output.getFormat());
    }
}
Also used : Image(com.jme3.texture.Image) BufferedImage(java.awt.image.BufferedImage) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D)

Aggregations

BufferedImage (java.awt.image.BufferedImage)1702 Graphics2D (java.awt.Graphics2D)376 IOException (java.io.IOException)220 File (java.io.File)195 FunctionException (lucee.runtime.exp.FunctionException)122 Graphics (java.awt.Graphics)104 ByteArrayOutputStream (java.io.ByteArrayOutputStream)89 Color (java.awt.Color)88 ByteArrayInputStream (java.io.ByteArrayInputStream)86 Test (org.junit.Test)81 WritableRaster (java.awt.image.WritableRaster)75 Point (java.awt.Point)71 Rectangle (java.awt.Rectangle)68 AffineTransform (java.awt.geom.AffineTransform)57 Image (java.awt.Image)56 InputStream (java.io.InputStream)51 Dimension (java.awt.Dimension)50 ImageIcon (javax.swing.ImageIcon)50 LayoutlibDelegate (com.android.tools.layoutlib.annotations.LayoutlibDelegate)46 ImageWriter (javax.imageio.ImageWriter)45