5fab9eb97b5ff4a0b2e1de06bc963fc310f8273c.svn-base 8.45 KB
package com.thinkgem.jeesite.modules.reg.web.bus;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.RequestMapping;


import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.reg.entity.bus.RegBusQlr;
import com.thinkgem.jeesite.modules.reg.entity.bus.RegBusSlsq;
import com.thinkgem.jeesite.modules.reg.service.bus.RegBusQlrService;
import com.thinkgem.jeesite.modules.reg.service.bus.RegBusSlsqService;

import sun.misc.BASE64Decoder;


@Controller
@RequestMapping(value = "${adminPath}/reg/bus/uploadExamPic")
public class uploadExamPicController extends BaseController{
	@Autowired
	private RegBusSlsqService regBusSlsqService;
	@Autowired
	private RegBusQlrService regBusQlrService;
	
	@RequestMapping(value = "scanss")
	public String scanss(RegBusSlsq regBusSlsq,HttpServletRequest request, HttpServletResponse response, Model model) {
		String picywh = request.getParameter("ywh");
		List<String> listpic = new ArrayList<String>();
		if(StringUtils.isNotBlank(picywh)){
		   String Path = Global.getPictures()+ picywh + "/";
		   File file = new File(Path);
		   if(file.exists()){
			File[] files = file.listFiles();
			if(files.length > 0){
				 for (File file2 : files) {
					 String absoltpath = (file2.getAbsolutePath()).replaceAll("\\\\", "/");
					 listpic.add(picywh+"/"+file2.getName());
					 logger.info(file2.getName());
				 }
			}
		}
		}
		Double data = Math.random();
		model.addAttribute("math", data);
		model.addAttribute("taskkey",request.getParameter("picid"));
		model.addAttribute("ywh", picywh);
		model.addAttribute("listpic",listpic);
		return "modules/reg/bus/dialog/scanss";
	}
	
	@RequestMapping(value = "scan")
	public String scan(RegBusSlsq regBusSlsq, HttpServletRequest request, HttpServletResponse response, Model model) {
			 String ywh = request.getParameter("ywh");
			 model.addAttribute("ywh", ywh);
		return "modules/reg/bus/dialog/scan";
	}
 @RequestMapping(value = "uploadPic")
  public void uploadPic(HttpServletRequest request,Model model,HttpServletResponse response) throws IOException{
      String ywh = request.getParameter("ywh");
      String sxh = request.getParameter("sxh");
	 String dirpic = Global.getPictures()+ywh;
	 //String savePath = "F:/images/";
	  File filedir = new File(dirpic);
	  String savePath = "";
	  if(filedir.isDirectory()){
		  savePath = dirpic + "/";
	  }else{
		  filedir.mkdir();
		  savePath = dirpic +"/";
	  }
		File tmp_path = new File(savePath);
		tmp_path.mkdirs();
		System.out.println("照片数据保存路径:" + savePath);
		int num = 100;
		//权利人人像比对是传递顺序号
		if(StringUtils.isNotBlank(sxh)) {
			num = Integer.parseInt(sxh);
		}
		String picname= Integer.toString(num);
		picname = ywh + "-"+picname+".jpg";
	     File file_txt = new File(savePath+picname);
	     if(StringUtils.isBlank(sxh)) {
	     do{//业务拍照
	    	 num = num+1;
	    	 picname= Integer.toString(num);
	    	 picname = ywh + "-"+ picname+".jpg";
	    	 file_txt = new File(savePath+picname);
	     }while(file_txt.exists());
	     }else {//人像比对拍照
	    	 picname= Integer.toString(num);
	    	 picname = ywh + "-"+ picname+".jpg";
	    	 file_txt = new File(savePath+picname);
	     }
	     
	    model.addAttribute("pic",picname);
      String image = request.getParameter("image");//240
      String type = request.getParameter("val");//240
      if(null!=image && !"".equals(image)){//防止没有拍照直接保存产生的空指针的错误
      	String width = request.getParameter("w");//320
      	String height = request.getParameter("h");//240
      	int w = Integer.parseInt(width);
      	int h = Integer.parseInt(height);
      	OutputStream out = null;
      	if (null!=type &&"pixel".equals(type)) {//Trident
	            try {
		            BufferedImage bf = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
		            String[] rows = image.split("\\|");
	  	            for (int i = 0; i < rows.length; i++) {
	  	                String[] col = rows[i].split(";");
	  	                for (int j = 0; j < col.length; j++) {
	  	                    int data = Integer.parseInt(col[j], 10);
	  	                    bf.setRGB(j, i, data);
	  	                }
	  	            }
		            File picPath = new File(savePath);
		            if (!picPath.exists()) {
		                picPath.mkdirs();
		            }
		            ImageIO.write(bf, "jpg", file_txt);
		         } catch (Exception e) {
		            e.printStackTrace();
		         }
      	} else {//WebKit
      		try {
		            byte[] bytes = null;
		            String imageStr = null;
	        		//Base64解码并生成图片  
	        		//替换头
	            	imageStr = image.replace("data:image/png;base64,", "").trim();
	            	/*if (null != image) {
	            		image = image.substring(image.indexOf(",") + 1);
	                }*/
	            	// Base64解码  
	        		bytes = new BASE64Decoder().decodeBuffer(imageStr);  
	        		for (int i = 0; i < bytes.length; ++i) {  
		        		if (bytes[i] < 0) {// 调整异常数据  
		        			bytes[i] += 256;  
		        		}  
	        		}  
					 if (!file_txt.exists()) {
						 file_txt.createNewFile();
					 }else{
						 file_txt.delete();
						 file_txt.createNewFile();
					 }
	        		// 生成jpeg图片  
	        		out = new FileOutputStream(file_txt.getAbsoluteFile());  
	        		out.write(bytes); 
		         } catch (Exception e) {
		            e.printStackTrace();
		         }finally{
		        	 if(null!=out){
		        		 out.flush();  
		        		 out.close(); 
		        	 }
      		}
      	}
      }else{
      	System.out.println("图像为空");
      }
      
      //人像比对附件保存后直接更新权利人字段
      if(StringUtils.isNotBlank(sxh)) {
    	  String tpbm = image.replace("data:image/png;base64,", "").trim();
    	  RegBusQlr regBusQlr = regBusQlrService.findByPic(ywh,sxh);
    	  regBusQlr.setTpbm(tpbm);
    	  regBusQlr.setPic(ywh+"/"+picname);
    	  regBusQlrService.save(regBusQlr);
      }
}
 
 @RequestMapping(value = "delpho")
 public String delpho(HttpServletRequest request,Model model,HttpServletResponse response) throws IOException{
	  String picpath = Global.getPictures();
	  String realpath = picpath + request.getParameter("picname");
	  File file =new File(realpath);
	  if(file.exists()){
		  file.delete();
	  }	
	  String sxh = request.getParameter("sxh");
	  String ywh = request.getParameter("ywh");
	  if(StringUtils.isBlank(sxh)) {
		 
	  return "redirect:"+Global.getAdminPath()+"/reg/bus/uploadExamPic/scanss?ywh="+ request.getParameter("ywh")+"&picid="+request.getParameter("picid");
	  }else {
		  RegBusQlr regBusQlr = regBusQlrService.findByPic(ywh,sxh);
    	  regBusQlr.setTpbm(null);
    	  regBusQlr.setPic(null);
    	  regBusQlrService.save(regBusQlr);
    return "redirect:"+Global.getAdminPath()+"/reg/bus/regBusQlr/rxbdForm?ywh="+ request.getParameter("ywh");
 
	  }
	  }
 
 @RequestMapping(value = "delphodir")
 public String delphodir(HttpServletRequest request,Model model,HttpServletResponse response) throws IOException{
	    //删除照片
	      String picpath =  Global.getPictures();
		  String dirpath = picpath + request.getParameter("ywh");
		  System.out.println(dirpath);
		  File filedir = new File(dirpath);
		  if(filedir.isDirectory()){
			  File[] filelist = filedir.listFiles();
			  for(int i = 0;i<filelist.length;i++){
				  filelist[i].delete();
			  }
				  filedir.delete();
		  }
		  return "redirect:"+ adminPath + "/act/task/todo/";
}
 
 
 
 
 //-----------------------------------------人像比对---------------------------------------------------
 @RequestMapping(value = "rxbdscan")
	public String rxbdscan(RegBusSlsq regBusSlsq, HttpServletRequest request, HttpServletResponse response, Model model) {
			 String ywh = request.getParameter("ywh");
			 String sxh = request.getParameter("sxh");
			 model.addAttribute("sxh", sxh);
			 model.addAttribute("ywh", ywh);
		return "modules/reg/bus/dialog/rxbdscan";
	}
}