博客
关于我
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/

    你可能感兴趣的文章
    python | fire,一个强大的 Python 库!
    查看>>
    python | flanker,一个神奇的 Python 库!
    查看>>
    python | flower,一个强大的 Python 库!
    查看>>
    python | funcy,一个超强的 提供函数式编程工具 Python 库!
    查看>>
    python | ggplot,一个超强的 Python 库!
    查看>>
    python | grab,一个强大的 Python 库!
    查看>>
    python | gunicorn,一个非常实用的 Python 库!
    查看>>
    python | h5py,一个无敌的关于 HDF5 的 Python 库!
    查看>>
    python | huey,一个非常厉害的 任务调度 Python 库!
    查看>>
    python | hypothesis,一个有趣的 Python 库!
    查看>>
    python | Indico,一个超酷的 Python 库!
    查看>>
    python | isort,一个有趣的 自动整理导入语句 的Python 库!
    查看>>
    python | jinja,一个超酷的 Python 库!
    查看>>
    python | joblib,一个强大的 Python 库!
    查看>>
    python调用git bash_Python学习第70课-用Git Bash在命令行打开sublime
    查看>>
    python | jsonschema,一个实用的 验证 JSON 数据结构 Python 库!
    查看>>
    python课程的中期报告范文_课题研究中期总结报告范文
    查看>>
    python | lxml,一个超酷的 关于XML/HTML 文档 Python 库!
    查看>>
    python | mplfinance,一个有趣的金融数据可视化 Python 库!
    查看>>
    python | nipy,一个强大的关于 神经影像数据分析 的Python 库!
    查看>>