当前位置 : 主页 > 编程语言 > c++ >

java 缩略图 ,裁剪图

来源:互联网 收集:自由互联 发布时间:2021-07-03
java 缩略图 ,裁剪图 // 原图BufferedImage src = ImageIO.read(new File("d:/a.jpg"));int sw = src.getWidth();int sh = src.getHeight();// 新的目标缩略图//指定尺寸 int w = 100;int h = 100;//宽度固定,高度按比例w =
java 缩略图 ,裁剪图
// 原图
			BufferedImage src = ImageIO.read(new File("d:/a.jpg"));
			int sw = src.getWidth();
			int sh = src.getHeight();

			// 新的目标缩略图
			//指定尺寸 
			int w = 100;
			int h = 100;
			
			//宽度固定,高度按比例
			w = 300;
			h = (int)(sh * ((float)w/sw));
			
			
			BufferedImage dst = new BufferedImage(w, h, 1);
			Graphics2D g = (Graphics2D) dst.getGraphics();

			g.drawImage(src, 0, 0, w, h, null);
			ImageIO.write(dst, "jpg", new File("d:/a_s.jpg"));





//裁剪
			public static void crop(String[] args) throws IOException {
				// 原图
				BufferedImage src = ImageIO.read(new File("d:/o.jpg"));
				int sw = src.getWidth();
				int sh = src.getHeight();
				
				//裁剪新图片
				int w = 150;
				int h = 150;
				BufferedImage dst = new BufferedImage(w, h, 1);
				Graphics2D g = (Graphics2D) dst.getGraphics();

				int x = 300;
				int y = 180;
				int xx = x+w;
				int yy = y+h;
				
				g.drawImage(src,0,0,w,h,x,y,xx,yy,null);
				ImageIO.write(dst, "jpg", new File("d:/ooo.jpg"));
				
			}
网友评论