Java 实现JPG图片BASE64子图裁剪抠图

Java 实现JPG图片BASE64子图裁剪抠图

public class ObjectCoordinate {/*** x 坐标*/private final double x;/*** y 坐标*/private final double y;public ObjectCoordinate(Double x, Double y) {this.x = x;this.y = y;}public double getX() {return x;}public double getY() {return y;}
}public class ObjectLocation {private final ObjectCoordinate upLeft;private final ObjectCoordinate loRight;public ObjectLocation(ObjectCoordinate up, ObjectCoordinate lo) {this.upLeft = up;this.loRight = lo;}public int getStartPointX() {return new Double(upLeft.getX()).intValue();}public int getLength() {return new Double(loRight.getX() - upLeft.getX()).intValue();}public int getStartPointY() {return new Double(upLeft.getY()).intValue();}public int getWidth() {return new Double(loRight.getY() - upLeft.getY()).intValue();}
}public interface ImageCutter {/*** 裁剪图片** @return*/String cut();
}public class Base64ImageCutter implements ImageCutter {private static final Logger logger = LoggerFactory.getLogger(Base64ImageCutter.class);private final String imageBase64Code;private final ObjectLocation objectLocation;public Base64ImageCutter(String imageBase64Code, ObjectLocation objectLocation) {this.imageBase64Code = imageBase64Code;this.objectLocation = objectLocation;}@Overridepublic String cut() {try {InputStream input = new ByteArrayInputStream(Base64Utils.decodeBuffer(imageBase64Code));Iterator it = ImageIO.getImageReadersByFormatName("jpg");ImageReader reader = it.next();ImageInputStream iis = ImageIO.createImageInputStream(input);reader.setInput(iis, true);ImageReadParam param = reader.getDefaultReadParam();Rectangle rect = new Rectangle(objectLocation.getStartPointX(), objectLocation.getStartPointY(), objectLocation.getLength(), objectLocation.getWidth());param.setSourceRegion(rect);BufferedImage bi = reader.read(0, param);ByteArrayOutputStream byt = new ByteArrayOutputStream();ImageIO.write(bi, "jpg", byt);byte[] bytes = byt.toByteArray();String subJpg = Base64Utils.encodeBuffer(bytes);return subJpg;} catch (Exception e) {logger.error("Image base 64 decode failed");throw new RuntimeException("Cut image exception");}}
}


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部