如何将网络图片水平反转后保存到本地

 public static void main(String[] args) {
 		// 需要镜像的图片路径
        String URLName = "http://xxx/file/electric/20220727143421.png";
        // 镜像后保存到本地的位置
        String target = "C:\IMAGE\new.png";
        try {
            getUrlImg( URLName, target);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public static void getUrlImg(String URLName,String target) throws Exception {
        URL url = new URL(URLName);
        URLConnection connection = url.openConnection();
        HttpURLConnection httpConnection  = (HttpURLConnection) connection;
        int httpResult = httpConnection.getResponseCode();
        // 判断是否连接成功;
        if (HttpResult != HttpURLConnection.HTTP_OK) {
            return;
        }
        int filesize = urlconn.getContentLength();
        int maxSize = 10240;//自定义最大可下载图片
        if(filesize  > maxSize){
        	return;
        }
        // 获取输入流
        BufferedInputStream bis = new BufferedInputStream(urlconn.getInputStream());
        // 输入流转图片文件
        BufferedImage image = ImageIO.read(bis);
        // 镜像处理
        BufferedImage imageNew = flipImage(image, 1);
        // 图片文件转输入流
        InputStream in =  bufferedImageToInputStream(imageNew);
        if (in == null){
            return;
        }
        // 创建输出流
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(target));
         // 输入流缓冲
        byte[] buffer = new byte[1024];
        int num = -1; 
        while (true) {
       		 // 读入到缓冲区
            num = biss.read(buffer); 
            if (num == -1) {
                bos.flush();
                break; 
            }
            bos.flush();
            bos.write(buffer, 0, num);
        }
        bos.close();
        bis.close();
        in .close();
    }


    /**
     * 将BufferedImage转换为InputStream
     * @param image
     * @return
     */
    public static InputStream bufferedImageToInputStream(BufferedImage image){
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        try {
            ImageIO.write(image, "png", os);
            return new ByteArrayInputStream(os.toByteArray());
        } catch (IOException e) {
            log.error("提示:",e);
        }
        return null;
    }

	/**
	* flipType 1 水平翻转 2 竖直翻转
	*/
    public static BufferedImage flipImage(BufferedImage source, Integer flipType) {
        log.info("图片翻转-开始。" + flipType);
        int width = source.getWidth();
        int height = source.getHeight();
        BufferedImage result = new BufferedImage(width, height, source.getType());
        Graphics graphics = result.getGraphics();
        try {
            if (flipType != null && flipType.equals(2)) {
                graphics.drawImage(source, 0, 0, width, height, 0, height, width, 0, null);
            } else {
                graphics.drawImage(source, 0, 0, width, height, width, 0, 0, height, null);
            }
        } catch (Exception e) {
            log.error("图片翻转-异常", e);
        } finally {
            graphics.dispose();
            source.flush();
        }
        log.info("图片翻转-结束");
        return result;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值