[Java] java设置图片像素 →→→→→进入此内容的聊天室

来自 , 2020-12-27, 写在 Java, 查看 127 次.
URL http://www.code666.cn/view/8a1e808b
  1. package cn.outofmemory.snippets.desktop;
  2.  
  3. import java.awt.Graphics2D;
  4. import java.awt.GraphicsConfiguration;
  5. import java.awt.GraphicsDevice;
  6. import java.awt.GraphicsEnvironment;
  7. import java.awt.Image;
  8. import java.awt.Toolkit;
  9. import java.awt.Transparency;
  10. import java.awt.image.BufferedImage;
  11. import java.awt.image.ImageObserver;
  12.  
  13. public class BufferedImagePixels {
  14.  
  15.   static boolean imageLoaded = false;
  16.  
  17.   public static void main(String[] args) {
  18.  
  19.       // The ImageObserver implementation to observe loading of the image
  20.       ImageObserver myImageObserver = new ImageObserver() {
  21.         public boolean imageUpdate(Image image, int flags, int x, int y, int width, int height) {
  22.           if ((flags & ALLBITS) != 0) {
  23.             imageLoaded = true;
  24.             System.out.println("Image loading finished!");
  25.             return false;
  26.           }
  27.           return true;
  28.         }
  29.       };
  30.  
  31.       // The image URL - change to where your image file is located!
  32.       String imageURL = "image.png";
  33.  
  34.       /*
  35.        * This call returns immediately and pixels are loaded in the background
  36.        * We use an ImageObserver to be notified when the loading of the image
  37.        * is complete
  38.        */
  39.       Image sourceImage = Toolkit.getDefaultToolkit().getImage(imageURL);
  40.       sourceImage.getWidth(myImageObserver);
  41.  
  42.       // We wait until the image is fully loaded
  43.       while (!imageLoaded) {
  44.           try {
  45.               Thread.sleep(100);
  46.           } catch (InterruptedException e) {
  47.           }
  48.       }
  49.  
  50.       // Create a buffered image from the source image with a format that's compatible with the screen
  51.       GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
  52.       GraphicsDevice graphicsDevice = graphicsEnvironment.getDefaultScreenDevice();
  53.       GraphicsConfiguration graphicsConfiguration = graphicsDevice.getDefaultConfiguration();
  54.  
  55.       // If the source image has no alpha info use Transparency.OPAQUE instead
  56.       BufferedImage image = graphicsConfiguration.createCompatibleImage(sourceImage.getWidth(null), sourceImage.getHeight(null), Transparency.BITMASK);
  57.  
  58.       // Copy image to buffered image
  59.       Graphics2D graphics = image.createGraphics();
  60.  
  61.       // Paint the image onto the buffered image
  62.       graphics.drawImage(sourceImage, 0, 0, null);
  63.       graphics.dispose();
  64.  
  65.       int x = 10;
  66.       int y = 10;
  67.  
  68.       // Get a pixel
  69.       int rgb = image.getRGB(x, y);
  70.  
  71.       System.out.println("Pixel at [" + x + "," + y + "] RGB : " + rgb);
  72.  
  73.       // Get all the pixels
  74.       int w = image.getWidth(null);
  75.       int h = image.getHeight(null);
  76.       int[] rgbs = new int[w*h];
  77.       image.getRGB(0, 0, w, h, rgbs, 0, w);
  78.  
  79.       // Set a pixel
  80.       rgb = 0xFF00FF00; // green
  81.       image.setRGB(x, y, rgb);
  82.  
  83.   }
  84.  
  85. }
  86.  
  87. //源代码片段来自云代码http://yuncode.net
  88.                        

回复 "java设置图片像素"

这儿你可以回复上面这条便签

captcha