package com.emr.controller;

import com.emr.util.ExceptionPrintUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.io.*;

/**
 * @ProjectName:
 * @Description:
 * @Param 传输参数
 * @Return
 * @Author: 曾文和
 * @CreateDate: 2020/6/5 15:14
 * @UpdateUser: 曾文和
 * @UpdateDate: 2020/6/5 15:14
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
@Controller
public class ImageController{
    /**
     * @param imgUrl 图片在本地磁盘的位置 如：E:/teacherCompetition/1/images/1.jpg
     * @param response
     */
    @RequestMapping("/toFindImg")
    public void picToJSP(@RequestParam("imgUrl") String imgUrl,HttpServletResponse response){
        InputStream in;
        response.setContentType("image/png;charset=utf-8");
        try {
            File file = new File(imgUrl);
            if(file.exists()) {
                //判断是否是jgp
                boolean flag = checkJpg(file);
                if (!flag) {
                    //tif转jpg输出到前台
                    //tiffTuanJPG(response.getOutputStream(), file);
                } else {
                    //图片读取路径
                    in=new FileInputStream(file);
                    int i=in.available();
                    byte[]data=new byte[i];
                    in.read(data);
                    in.close();
                    response.getOutputStream().write(data);
                }
            }
            //写图片
        } catch (Exception e) {
            ExceptionPrintUtil.printException(e);
            e.printStackTrace();
        }
    }
    private boolean checkJpg(File file){
        try {
            Image image = ImageIO.read(file);
            return image != null;
        } catch(IOException ex) {
            ExceptionPrintUtil.printException(ex);
            return false;
        }
    }
    /*private static void tiffTuanJPG(OutputStream out,File fileTiff){
        String filePath = fileTiff.getAbsolutePath();
        RenderedOp rd = JAI.create("fileload", filePath);//读取iff文件
        try {
            //文件存储输出流
            com.sun.media.jai.codec.JPEGEncodeParam param = new JPEGEncodeParam();
            ImageEncoder image = ImageCodec.createImageEncoder("JPEG", out, param);                         //指定输出格式
            image.encode(rd);
            //解析输出流进行输出
            out.close();
        } catch (IOException e) {
            ExceptionPrintUtil.printException(e);
            e.printStackTrace();
        }
    }*/
}
