BufferedImage类、Image类、Graphics类
BufferedImage
-
Image是一個(gè)抽象類,BufferedImage是其實(shí)現(xiàn)類,是一個(gè)帶緩沖區(qū)圖像類,主要作用是將一幅圖片加載到內(nèi)存中(BufferedImage生成的圖片在內(nèi)存里有一個(gè)圖像緩沖區(qū),利用這個(gè)緩沖區(qū)我們可以很方便地操作這個(gè)圖片),提供獲得繪圖對(duì)象、圖像縮放、選擇圖像平滑度等功能,通常用來做圖片大小變換、圖片變灰、設(shè)置透明不透明等。
public abstract Graphics getGraphics(); //獲得在圖像上繪圖的Graphics對(duì)象 -
Java將一幅圖片加載到內(nèi)存的方法是:
String imgPath = "C://demo.jpg"; BufferedImage image = ImageIO.read(new FileInputStream(imgPath)); -
繼而可以對(duì)圖片進(jìn)行操作,比如,獲得圖片的寬度:image.getWidth()
-
圖片只有加載到內(nèi)存中才能進(jìn)行進(jìn)一步的處理。
-
RGB:R(紅)G(綠)B(藍(lán))色彩模式是工業(yè)界的一種顏色標(biāo)準(zhǔn)。在Java中每個(gè)RGB像素所占的位數(shù)為8.
-
創(chuàng)建:
-
直接調(diào)用構(gòu)造函數(shù)
//指定寬高、圖像字節(jié)灰度
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY)
//創(chuàng)建一個(gè)不帶透明色的對(duì)象
BufferedImage bimage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//創(chuàng)建一個(gè)帶透明色的對(duì)象
new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); -
根據(jù)已經(jīng)存在的BufferedImage對(duì)象創(chuàng)建一個(gè)copy體
public BufferedImage createBufferedImage(BufferedImage src)
-
通過創(chuàng)建ColorModel(顏色轉(zhuǎn)換為Java中的像素表示)和Raster(光柵,描述像素的)對(duì)象創(chuàng)建BufferedImage對(duì)象
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;
}
-
讀取一個(gè)圖片文件來轉(zhuǎn)換.
BufferedImage image = ImageIo.read(new FileInputStream(filePath));
-
-
保存:找個(gè)位置寫出去
File outputfile = new File("save.png");ImageIO.write(bi,"png",outputfile); -
Raster和ColorModel對(duì)象、BufferedImage的創(chuàng)建與保存
ImageIO
- 提供read()和write()靜態(tài)方法,讀寫圖片,比以往的InputStream讀寫更方便。
BufferedImage與byte數(shù)組的轉(zhuǎn)換
-
在傳輸中,圖片是不能直接傳的,需要先轉(zhuǎn)為字節(jié)數(shù)組再傳輸較為方便;而字節(jié)數(shù)組再轉(zhuǎn)回BufferedImage則還原圖片。
-
BufferedImage–>byte[]
ImageIO.write(BufferedImage image,String format,OutputStream out);//format:圖片格式,“gif"等;//out:目標(biāo);特別的,如果目標(biāo)為byte數(shù)組,則將其預(yù)設(shè)為ByteArrayOutputStream即可傳入此方法,執(zhí)行完后,只要toByteArray()即可獲得byte[]. -
byte[]–>bufferedImage
ByteArrayInputStream in = new ByteArrayInputStream(byte[]b); //將b作為輸入流;BufferedImage image = ImageIO.read(InputStream in);//將in作為輸入流,讀取圖片存入image中,而這里in可以為ByteArrayInputStream(); -
參考文章
應(yīng)用
-
緩存網(wǎng)絡(luò)圖片
//獲得圖片地址Url img = new URL(url);//獲得圖片輸入流InputStream in = img.openStream();//把輸入流轉(zhuǎn)為BufferedImageJPEGImageDecoder decoderFile = JPEGCodec.createJPEGDecoder(in);BufferedImage image = decoderFile.decodeAsBufferedImage();//獲得其byte數(shù)組ImageIO.write(image, "jpg", bos);//寫出InputStream is = new ByteArrayInputStream(os.toByteArray()); -
具體測試與改動(dòng)
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
-
提供基本繪圖和顯示格式化文字的方法,畫圖用的坐標(biāo)系原點(diǎn)在左上角,縱軸向下。主要有畫線段、矩形、圓、橢圓、圓弧、多邊形等各種顏色的圖形、線條。
-
Graphics2D類提供更強(qiáng)大的繪圖能力。
-
在窗口畫一條直線: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);//畫一個(gè)點(diǎn) -
畫折線:drawPolyline(int[],int[],int),各點(diǎn)的x、y坐標(biāo),折線數(shù)。
-
畫字符串:drawString(String str,int x,int y),x、y是開始顯示的位置,使用默認(rèn)字體、大小、黑色。再寫下一行要寫在什么位置就很難精確定位了。若要精確定位,則需要知道字符串顯示的長度和字高,可以通過FontMetrics類來實(shí)現(xiàn)。
FontMetrics fm = g.getFontMetrics(font); //從Graphics對(duì)象獲取FontMetrics對(duì)象int height = fm.getHeight(); //調(diào)用其getHeight()獲得字高int width = fm.stringWidth(s1); //獲得字符串寬度 -
應(yīng)用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); -
顯示效果
-
設(shè)置畫筆字體:setFont(Font font);Java有一個(gè)類叫GraphicsEnvironment提供繪圖環(huán)境,其中g(shù)etAvailableFontFamilyNames()方法可獲取程序所在操作系統(tǒng)的所有字體名(是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); -
設(shè)置前景色(畫筆顏色):setColor(Color color),選擇顏色有兩種方法,一是直接用顏色值RGB創(chuàng)建Color對(duì)象:Color color=new Color(int R,int G,int B),由于是8位,所以不能超過255;二是用顏色常量如Color.red,Color.green等,Color類提供了13中顏色常量。
-
設(shè)置背景色:setBackground(new Color(int,int,int))
-
來個(gè)寫不同字體的小例子
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("現(xiàn)在是隸書" ,20, 120);g.setColor(new Color(0,0,0));} -
顯示效果
-
畫矩形:drawRect(int x,int y,int width,int height),畫矩形線框,x,y指定了左上角位置,后兩個(gè)為矩形寬高;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),線框,最后兩個(gè)寬高是圓角弧的橫向直徑和縱向直徑;fillRoundRect(int x,int y,int width,int height,int arcWidth,int arcHeight),顏色填充。
g.drawRoundRect(10,10,150,70,40,25);//畫一個(gè)圓角矩形g.setColor(Color.blue); g.fillRoundRect(80,100,100,100,60,40);//涂一個(gè)圓角矩形塊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),畫一個(gè)突出顯示的矩形(即3D矩形),raise是突出與否;fill3DRect(int x,int y,int width,int height,boolean raised),顏色填充。
g.draw3DRect(80,100,40,25,true);//畫一個(gè)線框g.setColor(Color.yellow); g.fill3DRect(20,70,20,30,true);//畫一個(gè)著色塊 -
畫橢圓:drawOval(int x,int y,int width,int height),x、y是中心坐標(biāo),長軸、短軸;fillOval(int x,int y,int width,int height),填充。
-
畫圓弧:drawArc(int x,int y,int width,int height,int startAngle,int arcAngle),畫橢圓一部分的圓弧線,橢圓中心時(shí)它的外接矩形的中心,外接矩形左上角坐標(biāo)為(x,y),寬width,高h(yuǎn)eight,startAngle單位是度,其實(shí)角度0度是指3點(diǎn)鐘方向,startAngle和arcAngle表示從startAngle角度開始,逆時(shí)針方向畫arcAngle度的弧,約定,正值度數(shù)是逆時(shí)針方向,負(fù)數(shù)為順時(shí)針,例如-90°是6點(diǎn)鐘方向;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),多邊形是多條線段首尾連接而成的封筆平面圖,多邊形線段端點(diǎn)的x,y坐標(biāo)存儲(chǔ)在兩個(gè)數(shù)組中,畫多邊形就是按給定的坐標(biāo)點(diǎn)順序用直線段將它們連起來,nPoints是坐標(biāo)點(diǎn)個(gè)數(shù);fillPolygon(int xPoints[],int yPoints[],int nPoints),著色。
int px1[]={50,90,10,50};//首末點(diǎn)相重,才能畫多邊形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);-
也可以用多邊形對(duì)象Polygon畫多邊形
- Polygon():創(chuàng)建多邊形對(duì)象,暫時(shí)沒有坐標(biāo)點(diǎn)。
- Polygon(int xPoints[],int yPoints[],int nPoints):用指定的坐標(biāo)點(diǎn)創(chuàng)建多邊形對(duì)象。
- addPoint():將一個(gè)坐標(biāo)點(diǎn)加入到Polygon對(duì)象中。
- drawPolygon(Polygon p):繪制多邊形。
- fillPolygon(Polygon p):和指定的顏色填充多邊形。
-
畫一個(gè)三角形
int x[]={140,180,170,180,140,100,110,100}; //用多邊形對(duì)象不要求首末點(diǎn)重合
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),當(dāng)需要在一個(gè)著色圖形中有一個(gè)空缺的矩形時(shí),可用背景色填充一矩形塊實(shí)現(xiàn),相當(dāng)于在該圖形上使用了橡皮擦。以下代碼實(shí)現(xiàn)了在一個(gè)圓中擦除了一個(gè)矩形塊
g.setColor(Color.blue);g.fillOval(50,50,100,100);g.clearRect(70,70,40,55); -
限定作圖顯示區(qū)域:clipRect(int x,int y,int width,int height),用一個(gè)矩形表示圖形的顯示區(qū)域,超出部分不顯示,多個(gè)限制區(qū)有覆蓋時(shí),得到交集區(qū)域
g.clipRect(0,0,100,50);g.clipRect(50,25,100,50); -
復(fù)制圖形:copyArea(int x,int y,int width,int height,int dx,int dy),dx和dy表示將圖形復(fù)制到原位置偏移的像素點(diǎn)數(shù),正值為往右或往下偏移,負(fù)值為往左或往上偏移,x、y是要復(fù)制矩形區(qū)域的左上角坐標(biāo)。以下代碼將一個(gè)矩形的部分、另一個(gè)矩形的全部分別平移
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); -
對(duì)Point、Rectangle類的應(yīng)用
Point p = new Point(cx / 2, cy / 2); //定義一個(gè)點(diǎn)Rectangle rect = new Rectangle((p.x - 40), (p.y - 40), 80, 40); //定義一個(gè)矩形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); -
畫圖形方法
代碼實(shí)例
- github/image_verifyCode分支/thz-parent/thz-manager-web/WebPageController、thz-common/tool/RandomValidateCodeUtil、RandomValidateCodeUtilTest
總結(jié)
以上是生活随笔為你收集整理的BufferedImage类、Image类、Graphics类的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 咸鱼菌玩3D—123D创建桌子
- 下一篇: JavaWeb网上拍卖系统jsp+sql