Java读取Oracle图片数据改变读取尺寸

  @RequestMapping(value = "loadRiderContentImg")//窗口大小  width: 590, height: 500,
    public void loadRiderContentImg(String RIDER_ID_, HttpServletResponse response, Map<String, Object> operator) {
        try {
            InputStream inputStream = riderService.loadRiderContent(RIDER_ID_);
            //读取图片输入流为 BufferedImage
            BufferedImage image = ImageIO.read(inputStream);
//            System.out.println("Width: " + image.getWidth());
//            System.out.println("Height: " + image.getHeight());

            //图片给宽度小于高度,已宽度为准
            int getImgMaxWidth = 540;
            if (image.getWidth() <= image.getHeight()) {
                if (image.getWidth() > getImgMaxWidth) {
                    //调整图片大小
                    Image resultingImage = image.getScaledInstance(Math.min(image.getWidth(), getImgMaxWidth), image.getHeight() * getImgMaxWidth / image.getWidth(), Image.SCALE_AREA_AVERAGING);
                    BufferedImage outputImage = new BufferedImage(Math.min(image.getWidth(), getImgMaxWidth), image.getHeight() * getImgMaxWidth / image.getWidth(), BufferedImage.TYPE_INT_RGB);
                    outputImage.getGraphics().drawImage(resultingImage, 0, 0, null);
                    //图像缓冲区图片保存为图片文件(文件不存在会自动创建文件保存,文件存在会覆盖原文件保存)
                    //ImageIO.write(outputImage, "jpg", new File("D:/pic2.jpg"));

                    ByteArrayOutputStream os = new ByteArrayOutputStream();
                    ImageIO.write(outputImage, "jpg", os);
                    inputStream = new ByteArrayInputStream(os.toByteArray());
                } else {
                    Image resultingImage = image.getScaledInstance(image.getWidth(), image.getHeight(), Image.SCALE_AREA_AVERAGING);
                    BufferedImage outputImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
                    outputImage.getGraphics().drawImage(resultingImage, 0, 0, null);

                    ByteArrayOutputStream os = new ByteArrayOutputStream();
                    ImageIO.write(outputImage, "jpg", os);
                    inputStream = new ByteArrayInputStream(os.toByteArray());
                }
            }

            //图片给宽度大于高度,已高度为准
            int getImgMaxHeight = 390;
            if (image.getWidth() > image.getHeight()) {
                if (image.getHeight() > getImgMaxHeight) {
                    Image resultingImage = image.getScaledInstance(image.getWidth() * getImgMaxHeight / image.getHeight(), Math.min(image.getWidth(), getImgMaxHeight), Image.SCALE_AREA_AVERAGING);
                    BufferedImage outputImage = new BufferedImage(image.getWidth() * getImgMaxHeight / image.getHeight(), Math.min(image.getWidth(), getImgMaxHeight), BufferedImage.TYPE_INT_RGB);
                    outputImage.getGraphics().drawImage(resultingImage, 0, 0, null);

                    ByteArrayOutputStream os = new ByteArrayOutputStream();
                    ImageIO.write(outputImage, "jpg", os);
                    inputStream = new ByteArrayInputStream(os.toByteArray());
                } else {
                    Image resultingImage = image.getScaledInstance(image.getWidth(), image.getHeight(), Image.SCALE_AREA_AVERAGING);
                    BufferedImage outputImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
                    outputImage.getGraphics().drawImage(resultingImage, 0, 0, null);

                    ByteArrayOutputStream os = new ByteArrayOutputStream();
                    ImageIO.write(outputImage, "jpg", os);
                    inputStream = new ByteArrayInputStream(os.toByteArray());
                }
            }

            response.reset();
            ServletOutputStream out = response.getOutputStream();
            byte[] content = new byte[65535];
            int length = 0;
            while ((length = inputStream.read(content)) != -1) {
                out.write(content, 0, length);
            }
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值