Example usage for java.awt.image BufferedImage getType

List of usage examples for java.awt.image BufferedImage getType

Introduction

In this page you can find the example usage for java.awt.image BufferedImage getType.

Prototype

public int getType() 

Source Link

Document

Returns the image type.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    BufferedImage originalImage = ImageIO.read(new File("test.png"));
    int type = originalImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : originalImage.getType();

    BufferedImage resizeImageBmp = resizeImage(originalImage, type);
    ImageIO.write(resizeImageBmp, "png", new File("a.png"));

    resizeImageBmp = resizeImageWithHint(originalImage, type);
    ImageIO.write(resizeImageBmp, "png", new File("b.png"));

}

From source file:Main.java

public static void main(String[] args) throws IOException {
    BufferedImage image = ImageIO.read(new File("E:/Java_Dev/plasma.gif"));

    // crop image
    BufferedImage firstHalf = image.getSubimage(0, 0, (image.getWidth() / 2), image.getHeight());
    BufferedImage secondHalf = image.getSubimage(image.getWidth() / 2, 0, image.getWidth() / 2,
            image.getHeight());/*from   w ww .  ja v a  2s.  c  o  m*/

    File croppedFile1 = new File("E:/Java_Dev/half1.png");
    File croppedFile2 = new File("E:/Java_Dev/half2.png");

    ImageIO.write(firstHalf, "png", croppedFile1);
    ImageIO.write(secondHalf, "png", croppedFile2);

    // join image
    BufferedImage joined = new BufferedImage(image.getWidth(), image.getHeight(), image.getType());
    BufferedImage image1 = ImageIO.read(new File("E:/Java_Dev/half1.png"));
    BufferedImage image2 = ImageIO.read(new File("E:/Java_Dev/half2.png"));

    Graphics2D graph = joined.createGraphics();
    graph.drawImage(image1, 0, 0, null);
    graph.drawImage(image2, image1.getWidth(), 0, null);

    File joinedFile = new File("E:/Java_Dev/joined.png");
    ImageIO.write(joined, "png", joinedFile);
}

From source file:Main.java

/**
 * Determine the appropriate {@link WritablePixelFormat} type that can
 * be used to transfer data into the indicated BufferedImage.
 * //from  w ww .  j  a  v  a  2s.co  m
 * @param bimg the BufferedImage that will be used as a destination for
 *             a {@code PixelReader<IntBuffer>#getPixels()} operation.
 * @return 
 */
private static WritablePixelFormat<IntBuffer> getAssociatedPixelFormat(BufferedImage bimg) {
    switch (bimg.getType()) {
    // We lie here for xRGB, but we vetted that the src data was opaque
    // so we can ignore the alpha.  We use ArgbPre instead of Argb
    // just to get a loop that does not have divides in it if the
    // PixelReader happens to not know the data is opaque.
    case BufferedImage.TYPE_INT_RGB:
    case BufferedImage.TYPE_INT_ARGB_PRE:
        return PixelFormat.getIntArgbPreInstance();
    case BufferedImage.TYPE_INT_ARGB:
        return PixelFormat.getIntArgbInstance();
    default:
        // Should not happen...
        throw new InternalError("Failed to validate BufferedImage type");
    }
}

From source file:Main.java

public static BufferedImage getFlippedImage(BufferedImage bi) {
    BufferedImage flipped = new BufferedImage(bi.getWidth(), bi.getHeight(), bi.getType());
    AffineTransform tran = AffineTransform.getTranslateInstance(0, bi.getHeight());
    AffineTransform flip = AffineTransform.getScaleInstance(1d, -1d);
    tran.concatenate(flip);//from  w  w  w  . j a v a 2  s  . c o  m

    Graphics2D g = flipped.createGraphics();
    g.setTransform(tran);
    g.drawImage(bi, 0, 0, null);
    g.dispose();

    return flipped;
}

From source file:Main.java

public static BufferedImage getStrokedImage(BufferedImage bi, Shape shape, int strokeWidth) {
    int w = bi.getWidth();
    int h = bi.getHeight();
    BufferedImage bib = new BufferedImage(w, h, bi.getType());
    Graphics2D g = bib.createGraphics();

    BasicStroke bs = new BasicStroke(strokeWidth);
    g.setStroke(bs);// w w  w .  j a va2 s.  c o m
    Rectangle rect = new Rectangle(0, 0, w, h);
    TexturePaint tp = new TexturePaint(bi, rect);
    g.setPaint(tp);
    g.draw(shape);

    g.dispose();
    return bib;
}

From source file:Main.java

/**
 * Quickly copies an image.//from  ww w.j a  v a2  s . co m
 * @param src The source image.
 * @return The replicated image.
 */
public static BufferedImage imgUtilFastCopy(BufferedImage src) {
    if (src == null)
        return null;
    BufferedImage b = new BufferedImage(src.getWidth(), src.getHeight(), src.getType());
    b.setData(src.getRaster());
    return b;
}

From source file:ImageUtil.java

/**
 * create new image from source image//from   w w w  .  j  a v  a 2 s .  com
 * @param srcImg source image
 * @param targetWidth target image width
 * @param targetHeight target image height
 * @return new image with specify width and height
 */
public static BufferedImage createNewImage(BufferedImage srcImg, int targetWidth, int targetHeight) {
    BufferedImage targetImg = null;
    int type = srcImg.getType();
    if (type == BufferedImage.TYPE_CUSTOM) {
        ColorModel cm = srcImg.getColorModel();
        WritableRaster raster = cm.createCompatibleWritableRaster(targetWidth, targetHeight);
        boolean alphaPremultiplied = cm.isAlphaPremultiplied();
        targetImg = new BufferedImage(cm, raster, alphaPremultiplied, null);
    } else {
        targetImg = new BufferedImage(targetWidth, targetHeight, type);
    }
    return targetImg;
}

From source file:Main.java

static BufferedImage enlarge(BufferedImage image, int n) {
    int w = image.getWidth() / n;
    int h = image.getHeight() / n;

    BufferedImage shrunkImage = new BufferedImage(w, h, image.getType());

    for (int y = 0; y < h; ++y)
        for (int x = 0; x < w; ++x)
            shrunkImage.setRGB(x, y, image.getRGB(x * n, y * n));

    return shrunkImage;
}

From source file:DBMS.UpdateFileUpload.java

public static boolean processFile(String path, FileItemStream item, int id) {
    try {//from   w  ww  .j  a  va2s.c  o  m
        String check = item.getName();
        if (check.endsWith(".jpg") || check.endsWith(".JPG")) {
            String imstring = "images/" + Integer.toString(id);
            File f = new File(path + File.separator + imstring);
            if (!f.exists())
                f.mkdir();
            File savedFile = new File(f.getAbsolutePath() + File.separator + item.getName());
            FileOutputStream fos = new FileOutputStream(savedFile);
            InputStream is = item.openStream();
            int x = 0;
            byte[] b = new byte[1024];
            while ((x = is.read(b)) != -1) {
                fos.write(b, 0, x);
            }
            fos.flush();
            fos.close();
            String dbimage = imstring + "/a.jpg";
            //dc.enterImage(dbimage);
            //im =dbimage;
            //System.out.println("Resizing!");
            //Resize rz = new Resize();
            //rz.resize(dbimage);
            BufferedImage originalImage = ImageIO.read(savedFile);
            int type = originalImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : originalImage.getType();
            BufferedImage resizeImageJpg = resizeImage(originalImage, type);
            ImageIO.write(resizeImageJpg, "jpg", savedFile);
            File rFile = new File(f.getAbsolutePath() + "/a.jpg");
            savedFile.renameTo(rFile);
            ProfileEditDB dc = new ProfileEditDB();
            dc.enterImage(id, dbimage);
            System.out.println("Link Entered to Database!");
            return true;
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}

From source file:Main.java

/**
 * Creates an outline of an image,/* w w w  .ja v  a2  s.co  m*/
 * with the default clipping rectangle.
 * @param src The source image.
 * @param c The color to outline the image
 * in.
 * @return
 */
public static BufferedImage imgUtilOutline(BufferedImage src, Color c) {
    if (src == null)
        return null;
    BufferedImage b = new BufferedImage(src.getWidth(), src.getHeight(), src.getType());
    Graphics2D g = (Graphics2D) b.getGraphics();
    g.setColor(c);
    g.drawRect(1, 1, src.getWidth() - 1, src.getHeight() - 1);
    g.drawImage(src, 0, 0, null);
    g.dispose();
    return b;
}