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); }
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); } }
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 main(String... args) throws Exception { BufferedImage image = ImageIO.read( new URL("http://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png")); int w = image.getWidth(); int h = image.getHeight(); int[] dataBuffInt = image.getRGB(0, 0, w, h, null, 0, w); Color c = new Color(dataBuffInt[100]); System.out.println(c.getRed()); // = (dataBuffInt[100] >> 16) & 0xFF System.out.println(c.getGreen()); // = (dataBuffInt[100] >> 8) & 0xFF System.out.println(c.getBlue()); // = (dataBuffInt[100] >> 0) & 0xFF System.out.println(c.getAlpha()); // = (dataBuffInt[100] >> 24) & 0xFF }
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(); } }
@Override public void paint(Graphics g) { super.paint(g); g.setColor(Color.WHITE); g.fillRect(0, 0, width, height); g.setColor(Color.BLACK); g.drawRect(0, 0, width - 1, height - 1); g.drawRect(1, 1, width - 3, height - 3); // g.setColor(Color.RED); // final String status = done + "/" + total; // g.drawString(status, width / 2, height / 2); g.drawImage(logo, width - logo.getWidth() - 4, height - logo.getHeight() - 4, null); drawProgessBar(g, done.intValue(), total.intValue()); final int nbErrors = errors.get(); if (nbErrors > 0) { g.setColor(Color.RED); final String message = "" + nbErrors + (nbErrors > 1 ? " diagrams" : " diagram") + " contains errors"; g.drawString(message, 10, 20); } g.setColor(link); final String urllink = "http://plantuml.com"; final Rectangle2D rect = getUsed(g, urllink); g.drawString(urllink, 10, (int) (height - rect.getMaxY())); limY = (int) (height - rect.getMaxY() + rect.getMinY()); limX = (int) (10 + rect.getMaxX()); }