传图片

package com.util;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;

/**
 * @Author: wangwx
 * @Description: 用于专递图片
 * @Date: 15:32 2020/12/2
 **/
public class ImgUtil {
    static BASE64Encoder encoder = new sun.misc.BASE64Encoder();
    static BASE64Decoder decoder = new sun.misc.BASE64Decoder();


    /**
     * @Author: wangwx
     * @Description: 获取本地图片
     * @Param: imgUrl 图片地址/C:\\Users\\1.jpg
     * @Return: String 图片Base64字符编码
     * @Date: 10:01 2020/12/3
     **/
    public static String getImgBASE64IO(String imgUrl) throws IOException{
        File file = new File(imgUrl);
        BufferedImage bi = ImageIO.read(file);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(bi,"jpg",baos);
        byte[] bytes = baos.toByteArray();
        return encoder.encode(bytes);
    }

    /**
     * @Author: wangwx
     * @Description: 获取网络图片
     * @Param: imgUrl 图片地址/http://192.168.0.0:8080/file/fs/l/118342236118323200
     * @Return: String 图片Base64字符编码
     * @Date: 10:01 2020/12/3
     **/
    public static String getImgBASE64FromNetwork(String imgUrl) throws IOException{
        URL url = new URL(imgUrl);
        BufferedImage bi = ImageIO.read(url);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(bi,"jpg",baos);
        byte[] bytes = baos.toByteArray();
        return encoder.encode(bytes);
    }

    /**
     * @Author: wangwx
     * @Description: 存放图片
     * @Param: imgBASE64String 图片字符编码
     * @Param: imgUrl 图片要存放的地址
     * @Return:
     * @Date: 10:03 2020/12/3
     **/
    public static void getImg(String imgBASE64String,String imgUrl) throws IOException{
        byte[] bytes = decoder.decodeBuffer(imgBASE64String);
        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
        BufferedImage bi = ImageIO.read(bais);
        File file = new File(imgUrl);
        ImageIO.write(bi,"jpg",file);
    }

    /**
     * @Author: wangwx
     * @Description: 显示图片
     * @Date: 10:08 2020/12/3
     **/
    public static void  main(String[] args) throws IOException {
        JFrame f = new JFrame();
        MyCanvas mc = new MyCanvas();

        String str = getImgBASE64IO("http://192.168.0.0:8080/file/fs/l/118342236118323200");
        byte[] bytes = decoder.decodeBuffer(str);
        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
        BufferedImage bi = ImageIO.read(bais);
        mc.setImg(bi);
        mc.repaint();
        f.add(mc);
        f.setSize(400,550);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
    }
}


class MyCanvas extends Canvas{
    private BufferedImage bi;
    private Image img;
    private int image_width;
    private int image_height;

    public void setImg(BufferedImage bi){
        this.bi=bi;
        this.zoom();
    }

    @Override
    public void paint(Graphics g){
        g.drawImage(img,0,0,this.getWidth(),this.getHeight(),this);
    }

    public void zoom(){
        image_width=bi.getWidth();
        image_height=bi.getHeight();
        img = bi.getScaledInstance(image_width,image_height,Image.SCALE_SMOOTH);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值