Image是一個抽象類,BufferedImage是其實現類,是一個帶緩沖區圖像類,主要作用是將一幅圖片加載到內存中(BufferedImage生成的圖片在內存里有一個圖像緩沖區,利用這個緩沖區我們可以很方便地操作這個圖片),提供獲得繪圖對象、圖像縮放、選擇圖像平滑度等功能,通常用來做圖片大小變換、圖片變灰、設置透明不透明等
Java將一幅圖片加載到內存的方法是:
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));
二、讀寫
File outputfile = new File("save.png");
ImageIO.write(bi,"png",outputfile);
Raster和ColorModel對象、BufferedImage的創建與保存
ImageIO
提供read()和write()靜態方法,讀寫圖片,比以往的InputStream讀寫更方便。
BufferedImage與byte數組的轉換
在傳輸中,圖片是不能直接傳的,需要先轉為字節數組再傳輸較為方便;而字節數組再轉回BufferedImage則還原圖片。
BufferedImage–>byte[]
ImageIO.write(BufferedImage image,String format,OutputStream out);
//format:圖片格式,“gif"等;
//out:目標;特別的,如果目標為byte數組,則將其預設為ByteArrayOutputStream即可傳入此方法,執行完后,只要toByteArray()即可獲得byte[].
byte[]–>bufferedImage
ByteArrayInputStream in = new ByteArrayInputStream(byte[]b); //將b作為輸入流;
BufferedImage image = ImageIO.read(InputStream in);
//將in作為輸入流,讀取圖片存入image中,而這里in可以為ByteArrayInputStream();
三、應用
緩存網絡圖片
//獲得圖片地址
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());
具體測試與改動
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)
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類來實現。
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);
顯示效果
設置畫筆字體:setFont(Font font);Java有一個類叫GraphicsEnvironment提供繪圖環境,其中getAvailableFontFamilyNames()方法可獲取程序所在操作系統的所有字體名(是String不是Font)。
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))
來個寫不同字體的小例子
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),指定填充顏色。
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),顏色填充。
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),顏色填充。
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),着色。
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),着色。
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),當需要在一個着色圖形中有一個空缺的矩形時,可用背景色填充一矩形塊實現,相當於在該圖形上使用了橡皮擦。以下代碼實現了在一個圓中擦除了一個矩形塊
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),用一個矩形表示圖形的顯示區域,超出部分不顯示,多個限制區有覆蓋時,得到交集區域
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是要復制矩形區域的左上角坐標。以下代碼將一個矩形的部分、另一個矩形的全部分別平移
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類的應用
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);
畫圖形方