|
@@ -211,38 +211,8 @@ public final class ImageUtil {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 裁剪图片
|
|
|
- *
|
|
|
- * @param img
|
|
|
- * @param dest
|
|
|
- * @param top
|
|
|
- * @param left
|
|
|
- * @param width
|
|
|
- * @param height
|
|
|
- * @return
|
|
|
- * @throws IOException
|
|
|
- */
|
|
|
- public static boolean cutImage(File img, String dest, int top, int left, int width, int height) throws IOException {
|
|
|
- File fileDest = new File(dest);
|
|
|
- if (!fileDest.getParentFile().exists())
|
|
|
- fileDest.getParentFile().mkdirs();
|
|
|
- String ext = getExtension(dest).toLowerCase();
|
|
|
- BufferedImage bi = (BufferedImage) ImageIO.read(img);
|
|
|
- height = Math.min(height, bi.getHeight());
|
|
|
- width = Math.min(width, bi.getWidth());
|
|
|
- if (height <= 0)
|
|
|
- height = bi.getHeight();
|
|
|
- if (width <= 0)
|
|
|
- width = bi.getWidth();
|
|
|
- top = Math.min(Math.max(0, top), bi.getHeight() - height);
|
|
|
- left = Math.min(Math.max(0, left), bi.getWidth() - width);
|
|
|
-
|
|
|
- BufferedImage bi_cropper = bi.getSubimage(left, top, width, height);
|
|
|
- return ImageIO.write(bi_cropper, ext.equals("png") ? "png" : "jpg", fileDest);
|
|
|
- }
|
|
|
|
|
|
- public static byte[] cutImage(byte[] img, String suff, int top, int left, int width, int height) {
|
|
|
+ public static byte[] cutImage(byte[] img, String suff, int x,int y, int width, int height) {
|
|
|
InputStream in = null;
|
|
|
BufferedImage bi = null;
|
|
|
ByteArrayOutputStream output = null;
|
|
@@ -255,10 +225,10 @@ public final class ImageUtil {
|
|
|
height = bi.getHeight();
|
|
|
if (width <= 0)
|
|
|
width = bi.getWidth();
|
|
|
- top = Math.min(Math.max(0, top), bi.getHeight() - height);
|
|
|
- left = Math.min(Math.max(0, left), bi.getWidth() - width);
|
|
|
+ x = Math.min(Math.max(0, x), bi.getWidth() - width);
|
|
|
+ y = Math.min(Math.max(0, y), bi.getHeight() - height);
|
|
|
|
|
|
- BufferedImage bi_cropper = bi.getSubimage(left, top, width, height);
|
|
|
+ BufferedImage bi_cropper = bi.getSubimage(x, y, width, height);
|
|
|
output = new ByteArrayOutputStream();
|
|
|
ImageIO.write(bi_cropper, suff, output);
|
|
|
byte[] ret = output.toByteArray();
|