Matlab中使用Java api画图图形并保存为jpeg格式
直接上代码:
close all; import java.io.*; import java.awt.*; import java.awt.image.BufferedImage; import java.awt.image.BufferedImage.*; import javax.imageio.ImageIO; h = 700; w = 500; image = BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); graphics = image.getGraphics(); graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); %对指定的矩形区域填充颜色 graphics.setColor(Color.WHITE); %GREEN:绿色。 红色:RED; 灰色:GRAY graphics.fillRect(0, 0, w, h); %对指定的矩形区域填充颜色 graphics.setColor(Color.WHITE); graphics.fillRect(240, 0, 240, 720); graphics.setColor(Color.BLACK); %画直线 x=100,y=100,x1=150,y1=y + 200; graphics.drawLine(x,y,x+x1,y1); %多边形 xPoints = [90,100,250,250]; yPoints = [180,150,150,180]; graphics.drawPolyline(xPoints, yPoints, 4); %椭圆 xOval=100,yOval=360; graphics.drawOval(xOval, yOval, 150, 130); %文字 graphics.drawString('直线',100+50,100-5); graphics.dispose(); ImageIO.write(image,'JPEG', File('testx.jpg')); img = imread('testx.jpg'); imshow(img);