前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >BufferedImage类、Image类、Graphics类

BufferedImage类、Image类、Graphics类

作者头像
Twcat_tree
发布2022-11-30 18:55:36
发布2022-11-30 18:55:36
1.3K00
代码可运行
举报
文章被收录于专栏:二猫の家二猫の家
运行总次数:0
代码可运行

BufferedImage

Image是一个抽象类,BufferedImage是其实现类,是一个带缓冲区图像类,主要作用是将一幅图片加载到内存中(BufferedImage生成的图片在内存里有一个图像缓冲区,利用这个缓冲区我们可以很方便地操作这个图片),提供获得绘图对象、图像缩放、选择图像平滑度等功能,通常用来做图片大小变换、图片变灰、设置透明不透明等。

代码语言:javascript
代码运行次数:0
运行
复制
  public abstract Graphics getGraphics(); //获得在图像上绘图的Graphics对象

Java将一幅图片加载到内存的方法是:

代码语言:javascript
代码运行次数:0
运行
复制
  String imgPath = "C://demo.jpg";  
  BufferedImage image = ImageIO.read(new FileInputStream(imgPath));

继而可以对图片进行操作,比如,获得图片的宽度:image.getWidth()

图片只有加载到内存中才能进行进一步的处理。

RGB:R(红)G(绿)B(蓝)色彩模式是工业界的一种颜色标准。在Java中每个RGB像素所占的位数为8.

创建:

  • 直接调用构造函数 //指定宽高、图像字节灰度 BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY) //创建一个不带透明色的对象 BufferedImage bimage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); //创建一个带透明色的对象 new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
  • 根据已经存在的BufferedImage对象创建一个copy体 public BufferedImage createBufferedImage(BufferedImage src)
  • 通过创建ColorModel(颜色转换为Java中的像素表示)和Raster(光栅,描述像素的)对象创建BufferedImage对象 public BufferedImage createBufferedImage(int width , int height, byte[] pixels){ //pixel像素 ColorModel cm = getColorModel(); SampleModel sm = getIndexSampleModel((IndexColorModel)cm, width,height); DataBuffer db = new DataBufferByte(pixels, width*height,0); WritableRaster raster = Raster.creatWritableRaster(sm, db,null); BufferedImage image = new BufferedImage (cm, raster,false, null); return image; }
  • 读取一个图片文件来转换. BufferedImage image = ImageIo.read(new FileInputStream(filePath));

保存:找个位置写出去

代码语言:javascript
代码运行次数:0
运行
复制
    File outputfile  = new File("save.png");
    ImageIO.write(bi,"png",outputfile);  

Raster和ColorModel对象、BufferedImage的创建与保存

ImageIO

  • 提供read()和write()静态方法,读写图片,比以往的InputStream读写更方便。

BufferedImage与byte数组的转换

在传输中,图片是不能直接传的,需要先转为字节数组再传输较为方便;而字节数组再转回BufferedImage则还原图片。

BufferedImage–>byte[]

代码语言:javascript
代码运行次数:0
运行
复制
  ImageIO.write(BufferedImage image,String format,OutputStream out);
  //format:图片格式,“gif"等;
  //out:目标;特别的,如果目标为byte数组,则将其预设为ByteArrayOutputStream即可传入此方法,执行完后,只要toByteArray()即可获得byte[].

byte[]–>bufferedImage

代码语言:javascript
代码运行次数:0
运行
复制
  ByteArrayInputStream in = new ByteArrayInputStream(byte[]b); //将b作为输入流;
  BufferedImage image = ImageIO.read(InputStream in);
  //将in作为输入流,读取图片存入image中,而这里in可以为ByteArrayInputStream();

参考文章

应用

缓存网络图片

代码语言:javascript
代码运行次数:0
运行
复制
  //获得图片地址
  Url img = new  URL(url);
  //获得图片输入流
  InputStream in = img.openStream();
  //把输入流转为BufferedImage
  JPEGImageDecoder decoderFile = JPEGCodec.createJPEGDecoder(in);
  BufferedImage image = decoderFile.decodeAsBufferedImage();
  //获得其byte数组
  ImageIO.write(image, "jpg", bos);
  //写出
  InputStream is = new ByteArrayInputStream(os.toByteArray());

具体测试与改动

代码语言:javascript
代码运行次数:0
运行
复制
  URL url = new URL("http://www.google.com/intl/en_ALL/images/logo.gif");
  BufferedImage image = ImageIO.read(url);
  ByteArrayOutputStream os = new ByteArrayOutputStream();
  ImageIO.write(image, "gif", os);
  InputStream is = new ByteArrayInputStream(os.toByteArray());

Graphics

提供基本绘图和显示格式化文字的方法,画图用的坐标系原点在左上角,纵轴向下。主要有画线段、矩形、圆、椭圆、圆弧、多边形等各种颜色的图形、线条。

Graphics2D类提供更强大的绘图能力。

在窗口画一条直线:drawLine(int x1,int y1,int x2,int y2)

代码语言:javascript
代码运行次数:0
运行
复制
  g.drawLine(3,3,50,50);//在(3,3)与(50,50)之间画一条线段
  g.drawLine(100,100,100,100);//画一个点

画折线:drawPolyline(int[],int[],int),各点的x、y坐标,折线数。

画字符串:drawString(String str,int x,int y),x、y是开始显示的位置,使用默认字体、大小、黑色。再写下一行要写在什么位置就很难精确定位了。若要精确定位,则需要知道字符串显示的长度和字高,可以通过FontMetrics类来实现。

代码语言:javascript
代码运行次数:0
运行
复制
  FontMetrics fm = g.getFontMetrics(font); //从Graphics对象获取FontMetrics对象
  int height = fm.getHeight(); //调用其getHeight()获得字高
  int width = fm.stringWidth(s1); //获得字符串宽度

应用FontMetrics精确定位

代码语言:javascript
代码运行次数:0
运行
复制
  String s1 = "Hello, Java World!";
  g.setColor(Color.red);
  setBackground(new Color(0,255,0));
  Font font = new Font("Arial", Font.BOLD, 18);
  g.setFont(font);
  FontMetrics fm = g.getFontMetrics(font);
  int height = fm.getHeight();
  int width = fm.stringWidth(s1);
  int posx =50; int posy = 50;
  g.drawString(s1 ,posx, posy);
  g.drawString("I will come in." ,posx +width, posy+height);

显示效果

设置画笔字体:setFont(Font font);Java有一个类叫GraphicsEnvironment提供绘图环境,其中getAvailableFontFamilyNames()方法可获取程序所在操作系统的所有字体名(是String不是Font)。

代码语言:javascript
代码运行次数:0
运行
复制
  GraphicsEnvironment gv =
  GraphicsEnvironment.getLocalGraphicsEnvironment();
  String[] ftNames = gv.getAvailableFontFamilyNames();
  for (int i=0; i<ftNames.length; i++)
      Font ft = new Font(ftNames[i], Font.BOLD, 14);

设置前景色(画笔颜色):setColor(Color color),选择颜色有两种方法,一是直接用颜色值RGB创建Color对象:Color color=new Color(int R,int G,int B),由于是8位,所以不能超过255;二是用颜色常量如Color.red,Color.green等,Color类提供了13中颜色常量。

设置背景色:setBackground(new Color(int,int,int))

来个写不同字体的小例子

代码语言:javascript
代码运行次数:0
运行
复制
  public void paint (Graphics g){
     String s1 = "This Font is ";
     Font font = new Font("Arial", Font.BOLD, 18);
     g.setColor(Color.red);
      setBackground(new Color(0,255,0));
     g.setFont(font);
     g.drawString(s1 + font.getName() ,20, 60);
     g.setFont(new Font("隶书", Font.BOLD, 28));
     g.drawString("现在是隶书" ,20, 120);
     g.setColor(new Color(0,0,0));
  }

显示效果

画矩形:drawRect(int x,int y,int width,int height),画矩形线框,x,y指定了左上角位置,后两个为矩形宽高;fillRect(iny x.int y,int width,int height),指定填充颜色。

代码语言:javascript
代码运行次数:0
运行
复制
  g.drawRect(80,100,40,25);//画线框
  g.setColor(Color.yellow);g.fillRect(20,70,20,30);//画着色块

画圆角矩形:drawRoundRect(int x,int y,int width,int height,int arcWidth,int arcHeight),线框,最后两个宽高是圆角弧的横向直径和纵向直径;fillRoundRect(int x,int y,int width,int height,int arcWidth,int arcHeight),颜色填充。

代码语言:javascript
代码运行次数:0
运行
复制
  g.drawRoundRect(10,10,150,70,40,25);//画一个圆角矩形
  g.setColor(Color.blue); g.fillRoundRect(80,100,100,100,60,40);//涂一个圆角矩形块
  g.drawRoundRect(10,150,40,40,40,40);//画圆
  g.setColor(Color.red); g.fillRoundRect(80,100,100,100,100,100);//画圆块

画三维矩形: draw3DRect(int x,int y,int width,int height,boolean raised),画一个突出显示的矩形(即3D矩形),raise是突出与否;fill3DRect(int x,int y,int width,int height,boolean raised),颜色填充。

代码语言:javascript
代码运行次数:0
运行
复制
  g.draw3DRect(80,100,40,25,true);//画一个线框
  g.setColor(Color.yellow); g.fill3DRect(20,70,20,30,true);//画一个着色块

画椭圆:drawOval(int x,int y,int width,int height),x、y是中心坐标,长轴、短轴;fillOval(int x,int y,int width,int height),填充。

画圆弧:drawArc(int x,int y,int width,int height,int startAngle,int arcAngle),画椭圆一部分的圆弧线,椭圆中心时它的外接矩形的中心,外接矩形左上角坐标为(x,y),宽width,高height,startAngle单位是度,其实角度0度是指3点钟方向,startAngle和arcAngle表示从startAngle角度开始,逆时针方向画arcAngle度的弧,约定,正值度数是逆时针方向,负数为顺时针,例如-90°是6点钟方向;fillArc(int x,int y,int width, int height, int startAngle, int arcAngle),着色。

代码语言:javascript
代码运行次数:0
运行
复制
  g.drawArc(10,40,90,50,0,180);//画圆弧线
  g.drawArc(100,40,90,50,180,180);//画圆弧线
  g.setColor(Color.yellow); g.fillArc(10,100,40,40,0,-270);//填充缺右上角的四分之三的椭圆
  g.setColor(Color.green); g.fillArc(60,110,110,60,-90,-270);//填充缺左下角的四分之三的椭圆

画多边形:drawPolygon(int xPoints[],int yPoints[],int nPoints),多边形是多条线段首尾连接而成的封笔平面图,多边形线段端点的x,y坐标存储在两个数组中,画多边形就是按给定的坐标点顺序用直线段将它们连起来,nPoints是坐标点个数;fillPolygon(int xPoints[],int yPoints[],int nPoints),着色。

代码语言:javascript
代码运行次数:0
运行
复制
  int px1[]={50,90,10,50};//首末点相重,才能画多边形
  int py1[]={10,50,50,10};
  int px2[]={140,180,170,180,140,100,110,140};
  int py2[]={5,25,35,45,65,35,25,5};
  g.setColor(Color.blue);
  g.fillPolygon(px1,py1,4);
  g.setColor(Color.red);
  g.drawPolygon(px2,py2,9);

   
也可以用多边形对象Polygon画多边形

     Polygon():创建多边形对象,暂时没有坐标点。Polygon(int xPoints[],int yPoints[],int nPoints):用指定的坐标点创建多边形对象。addPoint():将一个坐标点加入到Polygon对象中。drawPolygon(Polygon p):绘制多边形。fillPolygon(Polygon p):和指定的颜色填充多边形。

画一个三角形
int x[]={140,180,170,180,140,100,110,100}; //用多边形对象不要求首末点重合

int y[]={5,25,35,45,65,45,35,25};

Polygon ponlygon1=new Polygon();

polygon1.addPoint(50,10);

polygon1.addPoint(90,50);

polygon1.addPoint(10,50);

g.drawPolygon(polygon1);

g.setColor(Color.yellow);

Polygon polygon2 = new Polygon(x,y,8);

g.fillPolygon(polygon2);

画图片:drawImage(Image image,int x,int y)

擦除矩形块:clearREct(int x,int y,int width,int height),当需要在一个着色图形中有一个空缺的矩形时,可用背景色填充一矩形块实现,相当于在该图形上使用了橡皮擦。以下代码实现了在一个圆中擦除了一个矩形块

代码语言:javascript
代码运行次数:0
运行
复制
  g.setColor(Color.blue);
  g.fillOval(50,50,100,100);g.clearRect(70,70,40,55);

限定作图显示区域:clipRect(int x,int y,int width,int height),用一个矩形表示图形的显示区域,超出部分不显示,多个限制区有覆盖时,得到交集区域

代码语言:javascript
代码运行次数:0
运行
复制
  g.clipRect(0,0,100,50);g.clipRect(50,25,100,50);

复制图形:copyArea(int x,int y,int width,int height,int dx,int dy),dx和dy表示将图形复制到原位置偏移的像素点数,正值为往右或往下偏移,负值为往左或往上偏移,x、y是要复制矩形区域的左上角坐标。以下代码将一个矩形的部分、另一个矩形的全部分别平移

代码语言:javascript
代码运行次数:0
运行
复制
  g.drawRect(10,10,60,90);
  g.fillRect(90,10,60,90);
  g.copyArea(40,50,60,70,-20,80);
  g.copyArea(110,50,60,60,10,80);

对Point、Rectangle类的应用

代码语言:javascript
代码运行次数:0
运行
复制
  Point p = new Point(cx / 2, cy / 2); //定义一个点
  Rectangle rect = new Rectangle((p.x - 40), (p.y - 40), 80, 40); //定义一个矩形
  int[] xP = {(p.x - 40), (p.x + 90), p.x+200, (p.x - 40)};
  int[] yP = {(p.y - 40), (p.y +140), (p.y + 60), (p.y-40)};
  g.drawArc(rect.x, rect.y, rect.width, rect.height * 2, 270, 90); //画弧
  g.drawPolygon(xP, yP,3); //画多边形
  g.setColor(Color.red);

画图形方法

代码实例

  • github/image_verifyCode分支/thz-parent/thz-manager-web/WebPageController、thz-common/tool/RandomValidateCodeUtil、RandomValidateCodeUtilTest
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-06-23,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • BufferedImage
    • ImageIO
    • BufferedImage与byte数组的转换
    • 应用
    • Graphics
    • 代码实例
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档