首页 > 科普 / 正文
adjustparam publicstaticbyt

  public static byte[] compressImage(byte[] imageByte, int ppi) {

  byte[] smallImage=null;

  int width=0, height=0;

  if (imageByte==null)

  return null;

  ByteArrayInputStream byteInput=new ByteArrayInputStream(imageByte);

  try {

  Image image=ImageIO.read(byteInput);

  int w=image.getWidth(null);

  int h=image.getHeight(null);

  // adjust weight and height to avoid image distortion

  double scale=0;

  scale=Math.min((float) ppi / w, (float) ppi / h);

  width=(int) (w * scale);

  width -=width % 4;

  height=(int) (h * scale);

  if (scale >=(double) 1)

  return imageByte;

  BufferedImage buffImg=new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

  buffImg.getGraphics().drawImage(image.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);

  ByteArrayOutputStream out=new ByteArrayOutputStream();

  ImageIO.write(buffImg, "png", out);

  smallImage=out.toByteArray();

  return smallImage;

  } catch (IOException e) {

  log.error(e.getMessage());

  throw new RSServerInternalException("");

  }

  }

相关推荐

标签: id in In he 
Copyright ©2020-2023 东旭问答 | 网站备案号:蜀ICP备2022018493号-9 | 网站地图

声明: 文章来自网络,版权归原作者所有,如果有侵犯,请留言给我们及时删除 。