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

JAVA缩略图 ,剪裁图片

来源:互联网 收集:自由互联 发布时间:2021-06-30
gistfile1.txt // 原图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)(
gistfile1.txt
// 原图
			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"));
				
			}
上一篇:百度编辑器。txt
下一篇:@author Believer
网友评论