zoukankan      html  css  js  c++  java
  • Java后台获取Html5拍照的照片并下载的实例方法

      

    Html5目前渐渐的应用越来越广泛。它已经成为技术的一种主流。在国内的一些云平台,一些新的拍照技术上,也都运用了html5.我的这个实例是我们公司在云平台上用到的html5拍照并下载。

        这里前台html5怎么捕捉传数据的就不提了。后台接收,其实也比较简单,通过获取到前台传递的payload的形式的数据,看下面代码:

    View Code
    1 String cameraPhoto=String.valueOf(request.getParameter("data"));    //获取前台页面传过来的Base64加密的数据
    2                 String    saveFileNames=String.valueOf(request.getParameter("filename"));    //获取前台页面图片的名称
    3                 cameraPhoto=fileCommon.DelFormat(cameraPhoto);    //消除换行和空格
    4                 SimpleDateFormat dateFm = new SimpleDateFormat("yyyy-MM-ddhh:mm:ss"); //格式化当前系统
    5                 dateTime = dateFm.format(new java.util.Date()); //当前时间
    6                 picName=dateTime+saveFileNames;                          //现在图片的名称
    7                 urls = pros.getProperty("uploadUrl_uri")+ "savefile"+ File.separator+picName;    //最新的路径名称
    8                 long result=fileCommon.GenerateImage(cameraPhoto, urls);    //把数据转换成为图片

                      其实后台处理很简单,html5捕捉的图片,只是一组经过Base64加密过的数据,主要是看你怎么接收,接收时根据http协议的里面http正文的关键字(用谷歌浏览器控制台可以查看到),接收以后,解密,然后再把数据转换为图片就ok了。

    具体转换过程,看下面代码:

     

    View Code
     1 public class FileCommon {
     2     
     3     /*******************将图片转化为字符串***********************************/
     4     public static String GetImageStr(String imgFilePath) {
     5           // 将图片文件转化为字节数组字符串,并对其进行Base64编码处理
     6           byte[] data = null;
     7           // 读取图片字节数组
     8           try {
     9            InputStream in = new FileInputStream(imgFilePath);
    10            data = new byte[in.available()];
    11            in.read(data);
    12            in.close();
    13           } catch (IOException e) {
    14            e.printStackTrace();
    15           }
    16           // 对字节数组Base64编码
    17           BASE64Encoder encoder = new BASE64Encoder();
    18           return encoder.encode(data);// 返回Base64编码过的字节数组字符串
    19          }
    20         /***********************将字符串转化为图片***********************************************/
    21          public static long GenerateImage(String imgStr, String imgFilePath) {// 对字节数组字符串进行Base64解码并生成图片
    22           if (imgStr == null)
    23            // 图像数据为空
    24            return 0;
    25           BASE64Decoder decoder = new BASE64Decoder();
    26           try {
    27            // Base64解码
    28            byte[] bytes = decoder.decodeBuffer(imgStr);
    29            for (int i = 0; i < bytes.length; ++i) {
    30             if (bytes[i] < 0) {// 调整异常数据
    31                 bytes[i] += 256;
    32             }
    33            }
    34            // 生成jpeg图片
    35            OutputStream out = new FileOutputStream(imgFilePath);
    36            out.write(bytes);
    37            out.flush();
    38            out.close();
    39            return bytes.length;
    40           } catch (Exception e) {
    41               return 0;
    42           }
    43          }
    44     
    45     /********************字符串格式化,取消空格和换行******************************/
    46     public static String DelFormat(String param){
    47         Pattern p = Pattern.compile("\\s*|\t|\r|\n"); 
    48         Matcher ms = p.matcher(param); 
    49         param = ms.replaceAll("");
    50         String temps = "";
    51         // 把可能存在的换行剔除
    52         StringReader srs = new StringReader(param);
    53         BufferedReader brs = new BufferedReader(srs);
    54         String lines = null;
    55         try {
    56             while ((lines = brs.readLine()) != null) {
    57                 temps += lines;
    58             }
    59         } catch (IOException e1) {
    60             // TODO Auto-generated catch block
    61             e1.printStackTrace();
    62         }
    63         temps=temps.replace("\r\n", "\\r\\n");
    64         //去除换行和空格
    65         return temps;
    66     }
    67     /**
    68      * 获取当前时间的字符串
    69      * @return “yyyyMMddHHmmss”
    70      */
    71     public static String getTimeNow() {
    72         Date date = new Date();
    73         DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
    74         String str = dateFormat.format(date);
    75         return str;
    76     } 
    77 
    78 }

      

    经过以上处理,就可以成功的把照片存放到你要存放的位置了。

  • 相关阅读:
    vm12序列号
    三星手机官方固件下载
    MSTP故障处理手册
    分享一个高清壁纸网站
    ThinkPad X220 完美黑苹果 Hackintosh OS X 10.11 El Capitan
    一句命令激活windows/office
    Win10+VMplayer12中U盘无法挂载解决
    记一次金士顿DT100 G3 32G修复
    飘雪代码2枚
    禁用安全模式
  • 原文地址:https://www.cnblogs.com/shunxiyuan/p/2803378.html
Copyright © 2011-2022 走看看