zoukankan      html  css  js  c++  java
  • 处理图像

    java中不仅为图形绘制提供了丰富的方法,对图像处理也提供了非常方便的方法。

    一、加载与绘制

    java中进行图像处理的时候经常会用到Image(图像)类,该类对象是图像在java世界中的表示。Image类位于java.awt包中,其中一个抽象类,是java中所有表示图像类的超类。实际开发中一般使用不同类提供的工厂方法来加载图片文件,获取Image对象。

    1、Image类

                                                                    获取Image类对象的工厂方法

    方法签名 功能 所属类
    public Image getImage(java.net.URL url) 通过指定 的url加载图片获取Image对象,参数url表示需要加载图片的URL java.applet.Applet
    public Image getImage(java.net.URL url,String name 通过指定的URL加载图片获取Image对象,参数url表示需要加载图片所在目录的URL,name参数表示图片的文件名 java.applet.Applet
    Image getImage(java.net.URL url) 通过指定的URL加载图片获取Image对象,参数url表示需要加载图片所在目录的URL java.applet.AppletContext
    Image getImage(String filename) 通过指定的路径加载图片获取Image对象,参数filename为图像文件的路径字符串 java.awt.Toolkit
    Image getImage(URL url) 通过指定的URL加载图片获取Image对象,参数url表示需要加载图片的URL java.awt.Toolkit
    public Image getImage() 返回此ImageIcon对象所表示图像的Image对象 javax.swing.ImageIcon

    实际上各工厂方法获取的都是Image类某具体子类的对象,但一般情况下都统一作为Image类型返回进行操作。

                                                                                    Image类的方法表

     void flush()
              刷新此 Image 对象正在使用的所有可重构的资源。
     float getAccelerationPriority()
              返回加速优先级提示的当前值。
     ImageCapabilities getCapabilities(GraphicsConfiguration gc)
              返回 ImageCapabilities 对象,查询该对象便可以了解使用指定 GraphicsConfiguration 时此 Image 的功能。
    abstract  Graphics getGraphics()
              创建供绘制闭屏图像(off-screen image)使用的图形上下文。
    abstract  int getHeight(ImageObserver observer)
              确定图像的高度。
    abstract  Object getProperty(String name, ImageObserver observer)
              通过名称获取此图像的属性。
     Image getScaledInstance(int width, int height, int hints)
              创建此图像的缩放版本。
    abstract  ImageProducer getSource()
              获取生成图像像素的对象。
    abstract  int getWidth(ImageObserver observer)
              确定图像的宽度。
     void setAccelerationPriority(float priority)
              就加速的重要性设置此图像的提示。

                                                    Image类中表示图像缩放算法的常量表(getScaledInstance())

    常量名称 说明
    SCALE_AREA_AVERAGING 表示采用区域平均图像缩放算法
    SCALE_DEEAULT 表示采用默认的图像缩放算法
    SCALE_FAST 表示采用快速的图像缩放算法,此算法注重速度,但缩放的图像可能不是很平滑
    SCALE_REPLICATE 采用ReplicateScaleFilter类中包含的图像缩放算法
    SCALE_SMOOTH 表示采用侧重缩放效果的结果图像的平滑性不侧重速度的图像缩放算法

    2、Graphics类(画笔)

                                                        Graphics类中绘制图像的方法

    abstract  void clearRect(int x, int y, int width, int height)
              通过使用当前绘图表面的背景色进行填充来清除指定的矩形。
    abstract  void clipRect(int x, int y, int width, int height)
              将当前剪贴区与指定的矩形相交。
    abstract  void copyArea(int x, int y, int width, int height, int dx, int dy)
              将组件的区域复制到由 dxdy 指定的距离处。
    abstract  Graphics create()
              创建一个新的 Graphics 对象,它是此 Graphics 对象的副本。
     Graphics create(int x, int y, int width, int height)
              基于此 Graphics 对象创建一个新的 Graphics 对象,但是使用新的转换和剪贴区域。
    abstract  void dispose()
              释放此图形的上下文以及它使用的所有系统资源。
     void draw3DRect(int x, int y, int width, int height, boolean raised)
              绘制指定矩形的 3-D 高亮显示边框。
    abstract  void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle)
              绘制一个覆盖指定矩形的圆弧或椭圆弧边框。
     void drawBytes(byte[] data, int offset, int length, int x, int y)
              使用此图形上下文的当前字体和颜色绘制由指定 byte 数组给定的文本。
     void drawChars(char[] data, int offset, int length, int x, int y)
              使用此图形上下文的当前字体和颜色绘制由指定字符数组给定的文本。
    abstract  boolean drawImage(Image img, int x, int y, Color bgcolor, ImageObserver observer)
              绘制指定图像中当前可用的图像。
    abstract  boolean drawImage(Image img, int x, int y, ImageObserver observer)
              绘制指定图像中当前可用的图像。
    abstract  boolean drawImage(Image img, int x, int y, int width, int height, Color bgcolor, ImageObserver observer)
              绘制指定图像中已缩放到适合指定矩形内部的图像。
    abstract  boolean drawImage(Image img, int x, int y, int width, int height, ImageObserver observer)
              绘制指定图像中已缩放到适合指定矩形内部的图像。
    abstract  boolean drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, Color bgcolor, ImageObserver observer)
              绘制当前可用的指定图像的指定区域,动态地缩放图像使其符合目标绘制表面的指定区域。
    abstract  boolean drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, ImageObserver observer)
              绘制当前可用的指定图像的指定区域,动态地缩放图像使其符合目标绘制表面的指定区域。
    abstract  void drawLine(int x1, int y1, int x2, int y2)
              在此图形上下文的坐标系中,使用当前颜色在点 (x1, y1)(x2, y2) 之间画一条线。
    abstract  void drawOval(int x, int y, int width, int height)
              绘制椭圆的边框。
    abstract  void drawPolygon(int[] xPoints, int[] yPoints, int nPoints)
              绘制一个由 xy 坐标数组定义的闭合多边形。
     void drawPolygon(Polygon p)
              绘制由指定的 Polygon 对象定义的多边形边框。
    abstract  void drawPolyline(int[] xPoints, int[] yPoints, int nPoints)
              绘制由 xy 坐标数组定义的一系列连接线。
     void drawRect(int x, int y, int width, int height)
              绘制指定矩形的边框。
    abstract  void drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)
              用此图形上下文的当前颜色绘制圆角矩形的边框。
    abstract  void drawString(AttributedCharacterIterator iterator, int x, int y)
              依据 TextAttribute 类的规范应用指定迭代器的属性,呈现迭代器的文本。
    abstract  void drawString(String str, int x, int y)
              使用此图形上下文的当前字体和颜色绘制由指定 string 给定的文本。
     void fill3DRect(int x, int y, int width, int height, boolean raised)
              绘制一个用当前颜色填充的 3-D 高亮显示矩形。
    abstract  void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle)
              填充覆盖指定矩形的圆弧或椭圆弧。
    abstract  void fillOval(int x, int y, int width, int height)
              使用当前颜色填充外接指定矩形框的椭圆。
    abstract  void fillPolygon(int[] xPoints, int[] yPoints, int nPoints)
              填充由 xy 坐标数组定义的闭合多边形。
     void fillPolygon(Polygon p)
              用图形上下文的当前颜色填充指定 Polygon 对象定义的多边形。
    abstract  void fillRect(int x, int y, int width, int height)
              填充指定的矩形。
    abstract  void fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)
              用当前颜色填充指定的圆角矩形。
     void finalize()
              一旦不再引用此图形上下文就释放它。
    abstract  Shape getClip()
              获取当前剪贴区域。
    abstract  Rectangle getClipBounds()
              返回当前剪贴区域的边界矩形。
     Rectangle getClipBounds(Rectangle r)
              返回当前剪贴区域的边界矩形。
     Rectangle getClipRect()
              已过时。 从 JDK version 1.1 开始,由 getClipBounds() 取代。
    abstract  Color getColor()
              获取此图形上下文的当前颜色。
    abstract  Font getFont()
              获取当前字体。
     FontMetrics getFontMetrics()
              获取当前字体的字体规格。
    abstract  FontMetrics getFontMetrics(Font f)
              获取指定字体的字体规格。
     boolean hitClip(int x, int y, int width, int height)
              如果指定的矩形区域与当前的剪贴区域相交,则返回 true。
    abstract  void setClip(int x, int y, int width, int height)
              将当前剪贴区设置为由给定坐标指定的矩形。
    abstract  void setClip(Shape clip)
              将当前剪贴区域设置为任意的剪贴形状。
    abstract  void setColor(Color c)
              将此图形上下文的当前颜色设置为指定颜色。
    abstract  void setFont(Font font)
              将此图形上下文的字体设置为指定字体。
    abstract  void setPaintMode()
              设置将此图形上下文的绘图模式,以便通过此图形上下文中的当前颜色来覆盖目标。
    abstract  void setXORMode(Color c1)
              将此图形上下文的绘图模式设置为在此图形上下文的当前颜色和新的指定颜色之间交替。
     String toString()
              返回表示此 Graphics 对象值的 String 对象。
    abstract  void translate(int x, int y)
              将图形上下文的原点平移到当前坐标系中的点 (xy)。

    未完待续~~~以后补充(写完swing编程再来写)

  • 相关阅读:
    C#产生不重复随机数
    NT6 HDD Installer(硬盘装系统工具)装系统
    R语言实现 广义加性模型 Generalized Additive Models(GAM) 入门
    matlab小段代码学习
    java连接sql server2005
    python正则表达式
    Ubuntu下安装配置JDK 7
    mysql取代rand()的高效率随机读取方法
    UVA 1335 Beijing Guards
    杂思
  • 原文地址:https://www.cnblogs.com/xiaotiaosi/p/6393461.html
Copyright © 2011-2022 走看看