public Rect (BufferedImage source, int left, int top, int newWidth, int newHeight, boolean isPatch) { image = new BufferedImage(source.getColorModel(), source.getRaster().createWritableChild(left, top, newWidth, newHeight, 0, 0, null), source.getColorModel().isAlphaPremultiplied(), null); offsetX = left; offsetY = top; regionWidth = newWidth; regionHeight = newHeight; originalWidth = source.getWidth(); originalHeight = source.getHeight(); width = newWidth; height = newHeight; this.isPatch = isPatch; }
private BufferedImage trim9PBorder(BufferedImage inputImage) { BufferedImage trimedImage = new BufferedImage( inputImage.getWidth() - 2, inputImage.getHeight() - 2, BufferedImage.TYPE_INT_ARGB); Graphics2D g = trimedImage.createGraphics(); g.drawImage(inputImage, 0, 0, trimedImage.getWidth(), trimedImage.getHeight(), 1, 1, inputImage.getWidth() - 1, inputImage.getHeight() - 1, null); g.dispose(); return trimedImage; }
public void storeGreyscaleMapIntoAlpha(BufferedImage imageWithoutAlpha, BufferedImage greyscaleImage) { int width = imageWithoutAlpha.getWidth(); int height = imageWithoutAlpha.getHeight(); int[] imagePixels = imageWithoutAlpha.getRGB(0, 0, width, height, null, 0, width); int[] maskPixels = greyscaleImage.getRGB(0, 0, width, height, null, 0, width); for (int i = 0; i < imagePixels.length; i++) { int color = imagePixels[i] & 0x00ffffff; // Mask preexisting alpha int alpha = maskPixels[i] << 24; // Shift blue to alpha imagePixels[i] = color | alpha; } imageWithoutAlpha.setRGB(0, 0, width, height, imagePixels, 0, width); }
File path = ... // base path of the images // load source images BufferedImage image = ImageIO.read(new File(path, "image.png")); BufferedImage overlay = ImageIO.read(new File(path, "overlay.png")); // create the new image, canvas size is the max. of both image sizes int w = Math.max(image.getWidth(), overlay.getWidth()); int h = Math.max(image.getHeight(), overlay.getHeight()); BufferedImage combined = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); // paint both images, preserving the alpha channels Graphics g = combined.getGraphics(); g.drawImage(image, 0, 0, null); g.drawImage(overlay, 0, 0, null); // Save as new image ImageIO.write(combined, "PNG", new File(path, "combined.png"));
public static BufferedImage transformImage(BufferedImage image, AffineTransform transform) throws Exception { AffineTransformOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BICUBIC); BufferedImage destinationImage = op.createCompatibleDestImage(image, (image.getType() == BufferedImage.TYPE_BYTE_GRAY) ? image.getColorModel() : null ); Graphics2D g = destinationImage.createGraphics(); g.setBackground(Color.WHITE); g.clearRect(0, 0, destinationImage.getWidth(), destinationImage.getHeight()); destinationImage = op.filter(image, destinationImage); return destinationImage; }
/** * 在一个RGB画布上重新绘制Image,解决CMYK图像偏色的问题 */ public static BufferedImage redraw(BufferedImage img, Color bg) { BufferedImage rgbImage = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_3BYTE_BGR); Graphics2D g2d = rgbImage.createGraphics(); g2d.drawImage(img, 0, 0, bg, null); g2d.dispose(); return rgbImage; }
public static String ratioAsString(String src) throws IOException { File file = new File(src); if (!file.exists()) { return null; } BufferedImage bufferedImage = ImageIO.read(file); int width = bufferedImage.getWidth(); int height = bufferedImage.getHeight(); return String.format("%s x %s", width, height); }
private void verifyBorderImage(BufferedImage border) throws Wrong9PatchException { int[] rgb = border.getRGB(0, 0, border.getWidth(), border.getHeight(), null, 0, border.getWidth()); for (int i = 0; i < rgb.length; i++) { if ((0xff000000 & rgb[i]) != 0) { if (rgb[i] != 0xff000000 && rgb[i] != 0xffff0000) { throw new Wrong9PatchException(); } } } } }
public Rect (BufferedImage source, int left, int top, int newWidth, int newHeight, boolean isPatch) { image = new BufferedImage(source.getColorModel(), source.getRaster().createWritableChild(left, top, newWidth, newHeight, 0, 0, null), source.getColorModel().isAlphaPremultiplied(), null); offsetX = left; offsetY = top; regionWidth = newWidth; regionHeight = newHeight; originalWidth = source.getWidth(); originalHeight = source.getHeight(); width = newWidth; height = newHeight; this.isPatch = isPatch; }
public BufferedImage apply(BufferedImage img) { int width = img.getWidth(); int height = img.getHeight(); BufferedImage finalImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); Graphics2D g = finalImage.createGraphics(); g.setComposite(composite); g.drawImage(img, 0, 0, null); g.dispose(); return finalImage; }
private JComponent getBanner() { try { BufferedImage img = ImageIO.read(getClass().getResource("splash.png")); JLabel title = new JLabel(new ImageIcon(img)); title.setPreferredSize(new Dimension(img.getWidth() + 10, img.getHeight())); title.setOpaque(true); title.setBackground(new Color(238, 238, 255)); return title; } catch (Exception ignore) { } return new JLabel(); }
public static void makeGray(BufferedImage img) { for (int x = 0; x < img.getWidth(); ++x) for (int y = 0; y < img.getHeight(); ++y) { int rgb = img.getRGB(x, y); int r = (rgb >> 16) & 0xFF; int g = (rgb >> 8) & 0xFF; int b = (rgb & 0xFF); int grayLevel = (r + g + b) / 3; int gray = (grayLevel << 16) + (grayLevel << 8) + grayLevel; img.setRGB(x, y, gray); } }
private void generatePremultiplyAlpha(File out){ try { BufferedImage outImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB); float[] color = new float[4]; WritableRaster raster = image.getRaster(); WritableRaster outRaster = outImage.getRaster(); for(int x =0, w = image.getWidth(); x< w; ++x) for(int y =0, h = image.getHeight(); y< h; ++y){ raster.getPixel(x, y, color); float alpha = color[3]/255f; for(int i=0;i < 3; ++i) color[i] *= alpha; outRaster.setPixel(x, y, color); } ImageIO.write(outImage, "png", out); } catch (IOException e) { e.printStackTrace(); } }