博客
关于我
javaGUI学习15:AWT-无过滤图像处理
阅读量:328 次
发布时间:2019-03-04

本文共 3143 字,大约阅读时间需要 10 分钟。

Graphics类中的绘图方法

在Java AWT中,Graphics类提供了多个drawImage方法用于绘制图像。这些方法允许开发者以不同的方式缩放和绘制图像,满足不同的应用需求。以下是这些方法的简要说明:

  • 缩放绘图

    drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, Color bgcolor, ImageObserver observer)
    该方法绘制与当前可用的指定图像一样多的指定区域,并按比例缩放以适合目标可绘制表面。

  • 直接缩放绘图

    drawImage(Image img, int x, int y, int width, int height, Color bgcolor, ImageObserver observer)
    该方法绘制已经预先缩放好的图像,以适合指定的矩形区域。

  • 无背景色绘图

    drawImage(Image img, int x, int y, int width, int height, ImageObserver observer)
    该方法绘制尽可能多的指定图像,不使用背景色。

  • 默认缩放算法

    Image.getScaledInstance(int width, int height, int scale)
    该方法返回一个缩放后的图像实例,支持三种缩放算法:SCALE_DEFAULT(默认)、SCALE_FAST(速度优先)、SCALE_SMOOTH(平滑优先)和SCALE_REPLICATE(复制算法)。

  • PixelGrabber类:像素抓取接口

    PixelGrabber是一个实现ImageConsumer接口的类,用于从图像中抓取像素。它提供了多种方法来控制和获取像素数据。

    构造方法

    • PixelGrabber(ImageProducer ip, int x, int y, int w, int h, int[] pix, int off, int scansize)
      从指定的ImageProducer中抓取指定区域的像素,并将结果存储在给定的数组中。
    • PixelGrabber(Image img, int x, int y, int w, int h, boolean forceRGB)
      从指定图像中抓取指定区域的像素,默认使用RGB色彩模型。
    • PixelGrabber(Image img, int x, int y, int w, int h, int[] pix, int off, int scansize)
      从指定图像中抓取指定区域的像素,并将结果存储在给定的数组中。

    方法

    • void abortGrabbing()
      请求中止像素抓取操作。
    • ColorModel getColorModel()
      获取存储在像素数组中的颜色模型。
    • int getHeight()
      获取像素缓冲区的高度。
    • Object getPixels()
      获取像素缓冲区的内容。
    • int getStatus()
      返回当前像素抓取的状态。
    • int getWidth()
      获取像素缓冲区的宽度。
    • boolean grabPixels()
      请求开始像素抓取操作。
    • boolean grabPixels(long ms)
      请求开始像素抓取操作,并等待指定时间内完成。
    • void imageComplete(int status)
      通知像素抓取完成的状态。
    • void setColorModel(ColorModel model)
      设置用于获取像素的颜色模型。
    • void setDimensions(int width, int height)
      设置像素缓冲区的维度。
    • void setHints(int hints)
      提供关于像素抓取的提示信息。
    • void setPixels(int srcX, int srcY, int srcW, int srcH, ColorModel model, byte[] pixels, int srcOff, int srcScan)
      将像素数据设置到指定的缓冲区中。
    • void setProperties(Hashtable props)
      设置与像素抓取相关的属性。
    • void startGrabbing()
      请求开始像素抓取操作。
    • int status()
      返回当前像素抓取的状态。

    MemoryImageSource类:内存图像源

    MemoryImageSource是一个实现ImageProducer接口的类,用于从像素数组中生成图像数据。它支持多种构造方法,以适应不同的使用场景。

    构造方法

    • MemoryImageSource(int w, int h, int[] pix, int off, int scan)
      创建一个内存图像源,使用默认RGB颜色模型。
    • MemoryImageSource(int w, int h, ColorModel cm, byte[] pix, int off, int scan)
      创建一个内存图像源,使用指定的颜色模型和字节数组。
    • 其他构造方法类似于上述方法,提供了更多的灵活性。

    方法

    • void addConsumer(ImageConsumer ic)
      ImageConsumer添加到对此图像数据感兴趣的使用者列表中。
    • boolean isConsumer(ImageConsumer ic)
      检查指定的ImageConsumer是否在使用者列表中。
    • void newPixels()
      向所有订阅者提供一个新的像素缓冲区。
    • void newPixels(byte[] newpix, ColorModel newmodel, int offset, int scansize)
      更新像素缓冲区的内容。
    • void newPixels(int[] newpix, ColorModel newmodel, int offset, int scansize)
      更新像素缓冲区的内容。
    • void newPixels(int x, int y, int w, int h)
      向所有订阅者提供指定区域的像素缓冲区。
    • void newPixels(int x, int y, int w, int h, boolean framenotify)
      向所有订阅者提供指定区域的像素缓冲区,并通知动画完成。
    • void removeConsumer(ImageConsumer ic)
      从使用者列表中移除指定的ImageConsumer
    • void requestTopDownLeftRightResend(ImageConsumer ic)
      请求重新传输像素数据,按从上到下、从左到右的顺序。
    • void setAnimated(boolean animated)
      指定是否将此内存图像作为多帧动画或单帧静态图像。
    • void setFullBufferUpdates(boolean fullbuffers)
      指定是否始终通过完整的像素缓冲区来更新图像。
    • void startProduction(ImageConsumer ic)
      将指定的ImageConsumer添加到使用者列表中,并开始传输像素数据。

    使用MemoryImageSource裁剪图像

    通过MemoryImageSourcePixelGrabber类,可以轻松地从内存中生成图像,并进行裁剪和像素抓取操作。这种方法特别适用于需要动态调整图像大小或进行像素级处理的场景。

    转载地址:http://lalq.baihongyu.com/

    你可能感兴趣的文章
    Netty工作笔记0085---TCP粘包拆包内容梳理
    查看>>
    Netty常用组件一
    查看>>
    Netty常见组件二
    查看>>
    netty底层源码探究:启动流程;EventLoop中的selector、线程、任务队列;监听处理accept、read事件流程;
    查看>>
    Netty心跳检测机制
    查看>>
    Netty核心模块组件
    查看>>
    Netty框架内的宝藏:ByteBuf
    查看>>
    Netty框架的服务端开发中创建EventLoopGroup对象时线程数量源码解析
    查看>>
    Netty源码—2.Reactor线程模型一
    查看>>
    Netty源码—3.Reactor线程模型三
    查看>>
    Netty源码—4.客户端接入流程一
    查看>>
    Netty源码—4.客户端接入流程二
    查看>>
    Netty源码—5.Pipeline和Handler一
    查看>>
    Netty源码—5.Pipeline和Handler二
    查看>>
    Netty源码—6.ByteBuf原理一
    查看>>
    Netty源码—6.ByteBuf原理二
    查看>>
    Netty源码—7.ByteBuf原理三
    查看>>
    Netty源码—7.ByteBuf原理四
    查看>>
    Netty源码—8.编解码原理一
    查看>>
    Netty源码—8.编解码原理二
    查看>>