【精选】Java图片简单处理.docVIP

  • 1
  • 0
  • 约6.29千字
  • 约 8页
  • 2017-12-04 发布于贵州
  • 举报
【精选】Java图片简单处理

Java图片简单处理 本文档讲述Java对图片的一些简单处理。其中包括缩放图片、旋转绘制图片、忽略图片中某种颜色、切割图片、图片透明。 废话不多说,代码献上,然后在解释。 ImageUtil.java import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Image; import java.awt.image.BufferedImage; /** * 图片操作工具类 */ public final class ImageUtil { private ImageUtil() { } /** 设置颜色透明度(0~255) **/ public static final Color newColor(Color c, int alp) { return new Color(c.getRed(), c.getGreen(), c.getBlue(), alp); } /** 按倍率拉伸图片 **/ public static Image stretch(Image img, double rate) { if (img == null || rate = 0) return null; int w = img.getWidth(null); int h = img.getHeight(null); int width = (int) (w * rate); int height = (int) (h * rate); BufferedImage rImg = getSpaceImage(width, height, null); rImg.getGraphics() .drawImage(img, 0, 0, width, height, 0, 0, w, h, null); return rImg; } /** * 绘制旋转的图片br / * x,y 绘制起始坐标br / * rx,ry 旋转中心br / * ratio 旋转角度(0~360) */ public static final void revolve(Graphics g, Image img, int x, int y, int rx, int ry, int ratio) { Graphics2D g2d = (Graphics2D) g.create(); g2d.translate(x + rx, y + ry); g2d.rotate(Math.toRadians(ratio)); g2d.drawImage(img, -rx, -ry, null); } /** 忽略图片中的颜色,让该颜色呈现透明 **/ public static final void ignore(BufferedImage img, Color c) { if (c == null) return; for (int y = 0; y img.getHeight(); y++) { for (int x = 0; x img.getWidth(); x++) { if (img.getRGB(x, y) == c.getRGB()) { Color cc = new Color(img.getRGB(x, y)); img.setRGB(x, y, new Color(cc.getRed(), cc.getGreen(), cc.getBlue(), 0).getRGB()); } } } } /** 设置图片部分区域透明度(0~255) **/ public static final void alphaImage(BufferedImage img, int left, int top, int right, int bottom, int alp) { alp = alp 0 ? 0 : alp 255 ? 255 : alp; for (int ti = left; ti right; ti++) { for (int tj = top; tj bottom; tj++) { Color c = new Color(img.getRGB(ti, tj)); img.setRGB(ti, tj, new Color(c.getRed(), c.getGreen(),

文档评论(0)

1亿VIP精品文档

相关文档