java 设置图片像素,java设置图片像素,java图片像素,package cn.o

java设置图片像素,java图片像素,package cn.opackage 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); }}

输出:Image loading finished!Pixel at [10,10] RGB : -16294578

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值