JAVA中 BufferedImage、ImageIO用法
BufferedImage
BufferedImage是其Image抽象類的實現類,是一個帶緩沖區圖像類,主要作用是將一幅圖片加載到內存中(BufferedImage生成的圖片在內存里有一個圖像緩沖區,利用這個緩沖區我們可以很方便地操作這個圖片),提供獲得繪圖對象、圖像縮放、選擇圖像平滑度等功能,通常用來做圖片大小變換、圖片變灰、設置透明不透明等。
Java將一幅圖片輸入輸出:
// 將圖片讀入內存
String imgPath = "C://demo.jpg"; BufferedImage image = ImageIO.read( new FileInputStream(imgPath) ); // 保存圖片
File outputfile = new File("save.png"); ImageIO.write(bufferedImage, "png", outputfile);
創建BufferedImage對象:
//指定寬高、創建帶灰色的對象
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY); //創建一個不帶透明色的對象
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); //創建一個帶透明色的對象
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
BufferedImage 與 byte[] 數組的轉換
BufferedImage –> byte[]
/** * format:圖片格式,“gif"等; * 如果目標為byte數組,則將其預設為ByteArrayOutputStream即可傳入此方法,執行完后,只要toByteArray()即可獲得byte[] */ ImageIO.write(BufferedImage image, String format, OutputStream out);
byte[] –> bufferedImage
ByteArrayInputStream in = new ByteArrayInputStream(byte[] b); BufferedImage image = ImageIO.read(InputStream in);
BufferedImage 功能介紹
BufferedImage bufferedImage = ImageIO.read(new File("c:\\test.jpg")); // 獲取圖片的寬高
bufferedImage.getWidth(); bufferedImage.getHeight(); // 圖片裁剪
bufferedImage.getSubimage(0, 0, 10, 10); // 創建畫筆對象
Graphics2D graphics = bufferedImage.createGraphics();
Graphics2D 類提供強大的繪圖能力。
1、在窗口畫一條直線:drawLine(int x1,int y1,int x2,int y2)
g.drawLine(3,3,50,50);//在(3,3)與(50,50)之間畫一條線段
g.drawLine(100,100,100,100);//畫一個點
2、畫折線: drawPolyline(int[],int[],int),各點的x、y坐標,折線數。
3、畫字符串:drawString(String str,int x,int y),x、y是開始顯示的位置,使用默認字體、大小、黑色。再寫下一行要寫在什么位置就很難精確定位了。若要精確定位,則需要知道字符串顯示的長度和字高,可以通過FontMetrics類來實現。
FontMetrics fm = g.getFontMetrics(font); //從Graphics對象獲取FontMetrics對象
int height = fm.getHeight(); //調用其getHeight()獲得字高
int width = fm.stringWidth(s1); //獲得字符串寬度
應用FontMetrics精確定位
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);
消除字體鋸齒
// 消除邊緣抗鋸齒
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // 文本抗鋸齒
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
4、設置畫筆字體:setFont(Font font);
5、設置前景色(畫筆顏色):setColor(Color color),選擇顏色有兩種方法,一是直接用顏色值RGB創建Color對象:Color color=new Color(int R,int G,int B),由於是8位,所以不能超過255;二是用顏色常量如Color.red,Color.green等,Color類提供了13中顏色常量
6、設置背景色:setBackground(new Color(int,int,int))
7、畫矩形:drawRect(int x,int y,int width,int height),畫矩形線框,x,y指定了左上角位置,后兩個為矩形寬高;fillRect(iny x.int y,int width,int height),指定填充顏色。
g.drawRect(80,100,40,25);//畫線框
g.setColor(Color.yellow);
g.fillRect(20,70,20,30);//畫着色塊
8、畫圓角矩形: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),顏色填充。
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);//畫圓塊
9、畫三維矩形: draw3DRect(int x,int y,int width,int height,boolean raised),畫一個突出顯示的矩形(即3D矩形),raise是突出與否;fill3DRect(int x,int y,int width,int height,boolean raised),顏色填充。
g.draw3DRect(80,100,40,25,true);//畫一個線框
g.setColor(Color.yellow); g.fill3DRect(20,70,20,30,true);//畫一個着色塊
10、畫橢圓:drawOval(int x,int y,int width,int height), x、y是中心坐標,長軸、短軸;fillOval(int x,int y,int width,int height),填充。
11、畫圓弧: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),着色。
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);//填充缺左下角的四分之三的橢圓
12、畫多邊形:drawPolygon(int xPoints[],int yPoints[],int nPoints),多邊形是多條線段首尾連接而成的封筆平面圖,多邊形線段端點的x,y坐標存儲在兩個數組中,畫多邊形就是按給定的坐標點順序用直線段將它們連起來,nPoints是坐標點個數;fillPolygon(int xPoints[],int yPoints[],int nPoints),着色。
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):和指定的顏色填充多邊形。
13、畫一個三角形
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);
14、畫圖片:drawImage(Image image,int x,int y)
在圖片上畫圖片時(水印),設置與原圖組合方式
// 原圖和水印圖的組合方式,alpha:透明度0-->1
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
組合方式:
- AlphaComposite.CLEAR -交集部分的顏色和透明被清除。
- AlphaComposite.DST - 目標未修改。
- AlphaComposite.DST_ATOP - 目標和源重疊的部分組合在源上。
- AlphaComposite.DST_IN -顯示目標和源重疊的部分。
- AlphaComposite.DST_OUT -顯示目標沒有和源重疊的部分。
- AlphaComposite.DST_OVER - 目標覆蓋在源之上。
- AlphaComposite.SRC -源復制給目標。
- AlphaComposite.SRC_ATOP - 源和目標重疊的部分組合在目標上。
- AlphaComposite.SRC_IN - 顯示源和目標重疊的部分。
- AlphaComposite.SRC_OUT -顯示源沒有和目標重疊的部分。
- AlphaComposite.SRC_OVER - 源覆蓋在目標之上
在圖片上擦除矩形塊:clearREct(int x,int y,int width,int height),當需要在一個着色圖形中有一個空缺的矩形時,可用背景色填充一矩形塊實現,相當於在該圖形上使用了橡皮擦。以下代碼實現了在一個圓中擦除了一個矩形塊
g.setColor(Color.blue); g.fillOval(50,50,100,100);g.clearRect(70,70,40,55);
15、限定作圖顯示區域:clipRect(int x,int y,int width,int height),用一個矩形表示圖形的顯示區域,超出部分不顯示,多個限制區有覆蓋時,得到交集區域
g.clipRect(0,0,100,50);g.clipRect(50,25,100,50);
16、復制圖形:copyArea(int x,int y,int width,int height,int dx,int dy),dx和dy表示將圖形復制到原位置偏移的像素點數,正值為往右或往下偏移,負值為往左或往上偏移,x、y是要復制矩形區域的左上角坐標。以下代碼將一個矩形的部分、另一個矩形的全部分別平移
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);
19、對Point、Rectangle類的應用
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);