天天看點

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);