用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

java设置图片像素

2015-02-05 作者: java源代码大全举报

[java]代码库

package cn.outofmemory.snippets.desktop;

import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;

public class BufferedImagePixels {

  static boolean imageLoaded = false;

  public static void main(String[] args) {

      // The ImageObserver implementation to observe loading of the image
      ImageObserver myImageObserver = new ImageObserver() {
        public boolean imageUpdate(Image image, int flags, int x, int y, int width, int height) {
          if ((flags & ALLBITS) != 0) {
            imageLoaded = true;
            System.out.println("Image loading finished!");
            return false;
          }
          return true;
        }
      };

      // The image URL - change to where your image file is located!
      String imageURL = "image.png";

      /*
       * This call returns immediately and pixels are loaded in the background
       * We use an ImageObserver to be notified when the loading of the image
       * is complete
       */
      Image sourceImage = Toolkit.getDefaultToolkit().getImage(imageURL);
      sourceImage.getWidth(myImageObserver);

      // We wait until the image is fully loaded
      while (!imageLoaded) {
          try {
              Thread.sleep(100);
          } catch (InterruptedException e) {
          }
      }

      // Create a buffered image from the source image with a format that's compatible with the screen
      GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
      GraphicsDevice graphicsDevice = graphicsEnvironment.getDefaultScreenDevice();
      GraphicsConfiguration graphicsConfiguration = graphicsDevice.getDefaultConfiguration();

      // If the source image has no alpha info use Transparency.OPAQUE instead
      BufferedImage image = graphicsConfiguration.createCompatibleImage(sourceImage.getWidth(null), sourceImage.getHeight(null), Transparency.BITMASK);

      // Copy image to buffered image
      Graphics2D graphics = image.createGraphics();

      // Paint the image onto the buffered image
      graphics.drawImage(sourceImage, 0, 0, null);
      graphics.dispose();

      int x = 10;
      int y = 10;

      // Get a pixel
      int rgb = image.getRGB(x, y);

      System.out.println("Pixel at [" + x + "," + y + "] RGB : " + rgb);

      // Get all the pixels
      int w = image.getWidth(null);
      int h = image.getHeight(null);
      int[] rgbs = new int[w*h];
      image.getRGB(0, 0, w, h, rgbs, 0, w);

      // Set a pixel
      rgb = 0xFF00FF00; // green
      image.setRGB(x, y, rgb);

  }

}

//源代码片段来自云代码http://yuncode.net
			


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...