`
flashdream8
  • 浏览: 667171 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

java 切图 按比例切图

 
阅读更多

java 切图

 

public boolean scaleImageFromUrl(String oldpath, String newpath, int wdith,
			int height) {
		try {
			URL url = null;
			url = new URL(oldpath);
			URLConnection connection;
			connection = url.openConnection();
			BufferedImage bi = ImageIO.read(connection.getInputStream());
			Image Itemp = bi.getScaledInstance(wdith, height,
					BufferedImage.SCALE_SMOOTH);
			BufferedImage thumbnail = new BufferedImage(wdith, height,
					BufferedImage.TYPE_INT_RGB);
			thumbnail.getGraphics().drawImage(Itemp, 0, 0, null);
			File newimg = new File(newpath);
			FileOutputStream out = new FileOutputStream(newimg);
			JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
			JPEGEncodeParam param = encoder
			.getDefaultJPEGEncodeParam(thumbnail);
			param.setQuality(1.0f, false);
			encoder.encode(thumbnail);
			out.close();
			bi.flush();
			bi = null;
			return true;
		} catch (IOException e) {
			return false;
		}
	}

 

 

 

public boolean scaleImageFromUrl(String oldpath, String newpath, int wdith) {
		try {
			int height=367;
			URL url = null;
			url = new URL(oldpath);
			URLConnection connection;
			connection = url.openConnection();
			BufferedImage bi = ImageIO.read(connection.getInputStream());
			if(bi.getWidth()<=550){
				wdith=bi.getWidth();
				height=bi.getHeight();
			}else{
				height=(int)Math.floor(wdith*bi.getHeight()/bi.getWidth());
			}
			Image Itemp = bi.getScaledInstance(wdith, height,
					BufferedImage.SCALE_SMOOTH);
			BufferedImage thumbnail = new BufferedImage(wdith, height,
					BufferedImage.TYPE_INT_RGB);
			thumbnail.getGraphics().drawImage(Itemp, 0, 0, null);
			File newimg = new File(newpath);
			FileOutputStream out = new FileOutputStream(newimg);
			JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
			JPEGEncodeParam param = encoder
			.getDefaultJPEGEncodeParam(thumbnail);
			param.setQuality(0.8f, false);
			encoder.encode(thumbnail);
			out.close();
			bi.flush();
			bi = null;
			return true;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
	}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics