⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pixelgrabber.java

📁 gcc的组建
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* PixelGrabber.java -- retrieve a subset of an image's data   Copyright (C) 1999, 2003, 2004  Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING.  If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library.  Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule.  An independent module is a module which is not derived fromor based on this library.  If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so.  If you do not wish to do so, delete thisexception statement from your version. */package java.awt.image;import java.awt.Image;import java.util.Hashtable;/** * PixelGrabber is an ImageConsumer that extracts a rectangular region * of pixels from an Image. */public class PixelGrabber implements ImageConsumer{  int x, y, offset;  int width = -1;  int height = -1;  int scansize = -1;  boolean forceRGB = true;  ColorModel model = ColorModel.getRGBdefault();  int hints;  Hashtable props;  int int_pixel_buffer[];  boolean ints_delivered = false;  byte byte_pixel_buffer[];  boolean bytes_delivered = false;  ImageProducer ip;  int observerStatus;  int consumerStatus;  private Thread grabberThread;  boolean grabbing = false;  /**   * Construct a PixelGrabber that will retrieve RGB data from a given   * Image.   *   * The RGB data will be retrieved from a rectangular region   * <code>(x, y, w, h)</code> within the image.  The data will be   * stored in the provided <code>pix</code> array, which must have   * been initialized to a size of at least <code>w * h</code>.  The   * data for a pixel (m, n) in the grab rectangle will be stored at   * <code>pix[(n - y) * scansize + (m - x) + off]</code>.   *   * @param img the Image from which to grab pixels   * @param x the x coordinate, relative to <code>img</code>'s   * top-left corner, of the grab rectangle's top-left pixel   * @param y the y coordinate, relative to <code>img</code>'s   * top-left corner, of the grab rectangle's top-left pixel   * @param w the width of the grab rectangle, in pixels   * @param h the height of the grab rectangle, in pixels   * @param pix the array in which to store grabbed RGB pixel data   * @param off the offset into the <code>pix</code> array at which to   * start storing RGB data   * @param scansize a set of <code>scansize</code> consecutive   * elements in the <code>pix</code> array represents one row of   * pixels in the grab rectangle   */  public PixelGrabber(Image img, int x, int y, int w, int h,		      int pix[], int off, int scansize)  {    this (img.getSource(), x, y, w, h, pix, off, scansize);  }  /**   * Construct a PixelGrabber that will retrieve RGB data from a given   * ImageProducer.   *   * The RGB data will be retrieved from a rectangular region   * <code>(x, y, w, h)</code> within the image produced by   * <code>ip</code>.  The data will be stored in the provided   * <code>pix</code> array, which must have been initialized to a   * size of at least <code>w * h</code>.  The data for a pixel (m, n)   * in the grab rectangle will be stored at   * <code>pix[(n - y) * scansize + (m - x) + off]</code>.   *   * @param ip the ImageProducer from which to grab pixels   * @param x the x coordinate of the grab rectangle's top-left pixel,   * specified relative to the top-left corner of the image produced   * by <code>ip</code>   * @param y the y coordinate of the grab rectangle's top-left pixel,   * specified relative to the top-left corner of the image produced   * by <code>ip</code>   * @param w the width of the grab rectangle, in pixels   * @param h the height of the grab rectangle, in pixels   * @param pix the array in which to store grabbed RGB pixel data   * @param off the offset into the <code>pix</code> array at which to   * start storing RGB data   * @param scansize a set of <code>scansize</code> consecutive   * elements in the <code>pix</code> array represents one row of   * pixels in the grab rectangle   */  public PixelGrabber(ImageProducer ip, int x, int y, int w, int h,		      int pix[], int off, int scansize)  {    if (ip == null)      throw new NullPointerException("The ImageProducer must not be null.");    this.ip = ip;    this.x = x;    this.y = y;    this.width = w;    this.height = h;    this.offset = off;    this.scansize = scansize;    int_pixel_buffer = pix;    // Initialize the byte array in case ip sends us byte-formatted    // pixel data.    byte_pixel_buffer = new byte[pix.length * 4];  }  /**   * Construct a PixelGrabber that will retrieve data from a given   * Image.   *   * The RGB data will be retrieved from a rectangular region   * <code>(x, y, w, h)</code> within the image.  The data will be   * stored in an internal array which can be accessed by calling   * <code>getPixels</code>.  The data for a pixel (m, n) in the grab   * rectangle will be stored in the returned array at index   * <code>(n - y) * scansize + (m - x) + off</code>.   * If forceRGB is false, then the returned data will be not be   * converted to RGB from its format in <code>img</code>.   *   * If <code>w</code> is negative, the width of the grab region will   * be from x to the right edge of the image.  Likewise, if   * <code>h</code> is negative, the height of the grab region will be   * from y to the bottom edge of the image.   *   * @param img the Image from which to grab pixels   * @param x the x coordinate, relative to <code>img</code>'s   * top-left corner, of the grab rectangle's top-left pixel   * @param y the y coordinate, relative to <code>img</code>'s   * top-left corner, of the grab rectangle's top-left pixel   * @param w the width of the grab rectangle, in pixels   * @param h the height of the grab rectangle, in pixels   * @param forceRGB true to force conversion of the rectangular   * region's pixel data to RGB   */  public PixelGrabber(Image img,		      int x, int y,		      int w, int h,		      boolean forceRGB)  {    this.ip = img.getSource();    if (this.ip == null)      throw new NullPointerException("The ImageProducer must not be null.");    this.x = x;    this.y = y;    width = w;    height = h;    // If width or height is negative, postpone pixel buffer    // initialization until setDimensions is called back by ip.    if (width >= 0 && height >= 0)      {	int_pixel_buffer = new int[width * height];	byte_pixel_buffer = new byte[width * height];      }    this.forceRGB = forceRGB;  }  /**   * Start grabbing pixels.   *   * Spawns an image production thread that calls back to this   * PixelGrabber's ImageConsumer methods.   */  public synchronized void startGrabbing()  {    // Make sure we're not already grabbing.    if (grabbing == false)      {	grabbing = true;	grabberThread = new Thread ()	  {	    public void run ()	    {              try                {                  ip.startProduction (PixelGrabber.this);                }              catch (Exception ex)                {                  ex.printStackTrace();                  imageComplete(ImageConsumer.IMAGEABORTED);                }	    }	  };	grabberThread.start ();      }  }  /**   * Abort pixel grabbing.   */  public synchronized void abortGrabbing()  {    if (grabbing)      {	// Interrupt the grabbing thread.        Thread moribund = grabberThread;        grabberThread = null;        moribund.interrupt();	imageComplete (ImageConsumer.IMAGEABORTED);      }  }  /**   * Have our Image or ImageProducer start sending us pixels via our   * ImageConsumer methods and wait for all pixels in the grab   * rectangle to be delivered.   *   * @return true if successful, false on abort or error   *   * @throws InterruptedException if interrupted by another thread.   */  public synchronized boolean grabPixels() throws InterruptedException  {    return grabPixels(0);  }  /**   * grabPixels's behavior depends on the value of <code>ms</code>.   *   * If ms < 0, return true if all pixels from the source image have   * been delivered, false otherwise.  Do not wait.   *   * If ms >= 0 then we request that our Image or ImageProducer start   * delivering pixels to us via our ImageConsumer methods.   *   * If ms > 0, wait at most <code>ms</code> milliseconds for   * delivery of all pixels within the grab rectangle.   *   * If ms == 0, wait until all pixels have been delivered.   *   * @return true if all pixels from the source image have been   * delivered, false otherwise   *   * @throws InterruptedException if this thread is interrupted while   * we are waiting for pixels to be delivered   */  public synchronized boolean grabPixels(long ms) throws InterruptedException  {    if (ms < 0)      return ((observerStatus & (ImageObserver.FRAMEBITS				 | ImageObserver.ALLBITS)) != 0);    // Spawn a new ImageProducer thread to send us the image data via    // our ImageConsumer methods.    startGrabbing();    if (ms > 0)      {	long stop_time = System.currentTimeMillis() + ms;	long time_remaining;	while (grabbing)	  {	    time_remaining = stop_time - System.currentTimeMillis();	    if (time_remaining <= 0)	      break;	    wait (time_remaining);	  }	abortGrabbing ();      }    else      wait ();    // If consumerStatus is non-zero then the image is done loading or    // an error has occurred.    if (consumerStatus != 0)      return setObserverStatus ();    return ((observerStatus & (ImageObserver.FRAMEBITS			       | ImageObserver.ALLBITS)) != 0);  }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -