下面一段是我上传图片的功能,求大神帮我加一段,就是比如我上传一个10M的图片,改变它的大小,改小点。 String ext = fileFileName.substring(fileFileName.lastIndexOf(“”.””)+1).toLowerCase(); //图片后缀名 String fileName = img.getId()+”.”+ext; //新文件名 //保存 return pathdir+”/”+fileName; |
|
20分 |
|
原理应该是压缩
|
|
新手,在线等高手修改代码。
|
|
不要沉啊~求大神现身
|
|
1楼不是给了 方法的链接么?
|
|
80分 |
@SuppressWarnings("finally") public static byte[] scale(byte[] bytes , double width, double height) { BufferedImage bufferedImage = null; BufferedImage bufTarget = null; try { ByteArrayInputStream bais = new ByteArrayInputStream(bytes); bufferedImage = ImageIO.read(bais); double sx = width / bufferedImage.getWidth(); double sy = height / bufferedImage.getHeight(); int type = bufferedImage.getType(); if (type == BufferedImage.TYPE_CUSTOM) { ColorModel cm = bufferedImage.getColorModel(); WritableRaster raster = cm.createCompatibleWritableRaster((int)width, (int)height); boolean alphaPremultiplied = cm.isAlphaPremultiplied(); bufTarget = new BufferedImage(cm, raster, alphaPremultiplied, null); } else { bufTarget = new BufferedImage((int)width, (int)height, type); } Graphics2D g = bufTarget.createGraphics(); g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); g.drawRenderedImage(bufferedImage, AffineTransform.getScaleInstance(sx, sy)); g.dispose(); if(bufTarget != null){ ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(bufTarget, "jpeg", baos); byte[] result = baos.toByteArray(); return result; } } catch (IOException e) { e.printStackTrace(); } return null; } 图片压缩,把图片的byte字节和需要重新生成的大小传递进来即可. |
图片压缩工具类,点一楼给你的链接进去,有你想要的
|
|
图片压缩,如果采用zip类型的压缩的话,那么其效率确实不高,你可以采用其他的先压缩在上传,或者是切割上传,然后在后台进行组装
|