public abstract Graphics getGraphics(); //获得在图像上绘图的Graphics对象
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);
public BufferedImage createBufferedImage(BufferedImage src)
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);
ImageIO.write(BufferedImage image,String format,OutputStream out);
//format:图片格式,“gif"等;
//out:目标;特别的,如果目标为byte数组,则将其预设为ByteArrayOutputStream即可传入此方法,执行完后,只要toByteArray()即可获得byte[].
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());
g.drawLine(3,3,50,50);//在(3,3)与(50,50)之间画一条线段
g.drawLine(100,100,100,100);//画一个点
FontMetrics fm = g.getFontMetrics(font); //从Graphics对象获取FontMetrics对象
int height = fm.getHeight(); //调用其getHeight()获得字高
int width = fm.stringWidth(s1); //获得字符串宽度
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);
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);
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));
}
g.drawRect(80,100,40,25);//画线框
g.setColor(Color.yellow);g.fillRect(20,70,20,30);//画着色块
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);//画圆块
g.draw3DRect(80,100,40,25,true);//画一个线框
g.setColor(Color.yellow); g.fill3DRect(20,70,20,30,true);//画一个着色块
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);//填充缺左下角的四分之三的椭圆
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);
g.setColor(Color.blue);
g.fillOval(50,50,100,100);g.clearRect(70,70,40,55);
g.clipRect(0,0,100,50);g.clipRect(50,25,100,50);
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 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);
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://lebron.blog.csdn.net/article/details/125754579
内容来源于网络,如有侵权,请联系作者删除!