camera.swf java_java调用摄像头保存图片上传功能

在项目中要用到jsp页面实现网页采集摄像头图像,并实现上传的功能,页面主要js调用的,所以可以使用多种语言php,asp等都可以使用,而且是跨浏览器的。

可以整合的到SSH项目中和struts1或者struts2项目中

使用方法1:

下载 printscreen4web-jsp.war(最下面有下载地址),然后放到tamcat目录webapp文件夹下面,启动tomcat,访问http://127.0.0.1:8080/printscreen4web-jsp/

主要调用文件 :1.index.html.2avatar.jsp

String imagepath1="";

//编辑页面中包含 camera.swf 的 HTML 代码

public String renderHtml(String id,String basePath,String input)

{

String outinput="";

try {

outinput = URLDecoder.decode(input);

}catch(Exception e)

{

System.out.println("解码错误!");

}

String[] tmp_input=outinput.split("@");//input传递的类型和uid

//System.out.println(input+","+tmp_input.length);

String uc_api =URLEncoder.encode(basePath+"upload/avatar.jsp");

String urlCameraFlash = "camera.swf?nt=1&inajax=1&appid=1&input="+input+"&uploadSize=1000&ucapi="+uc_api;

urlCameraFlash = "";

return urlCameraFlash;

}

public String getFileExt(String fileName) {

// 下面取到的扩展名错误,只有三位,而如html的文件则有四位

// extName = fileName.substring(fileName.length() - 3, fileName.length()); //扩展名

int dotindex = fileName.lastIndexOf(".");

String extName = fileName.substring(dotindex, fileName.length());

extName = extName.toLowerCase(); //置为小写

return extName;

}

private byte[] getFlashDataDecode(String src)

{

char []s=src.toCharArray();

int len=s.length;

byte[] r = new byte[len / 2];

for (int i = 0; i < len; i = i + 2)

{

int k1 = s[i] - 48;

k1 -= k1 > 9 ? 7 : 0;

int k2 = s[i + 1] - 48;

k2 -= k2 > 9 ? 7 : 0;

r[i / 2] = (byte)(k1 << 4 | k2);

}

return r;

}

public boolean saveFile(String path,byte[]b){

try{

FileOutputStream fs = new FileOutputStream(path);

fs.write(b, 0, b.length);

fs.close();

return false;

}catch(Exception e){

return true;

}

}

%>

String uploadtype= request.getParameter("uploadtype");

//图片上传类型:头像,logo,图片分别对应参数:head,logo,pic没有传递,默认为PIC

if("".equals(uploadtype))

{

uploadtype="pic";

}

//最终裁剪好的图片存放位置

String uid=request.getParameter("uid");

imagepath1=uploadtype+"/"+uid+"_big.jpg";

String action= request.getParameter("a");

//String input=request.getParameter("input");

String path = request.getContextPath();

String infoFilePath="";

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

if(action==null){

%>

body{

margin: 0;

border: 0;

}

function updateavatar() {

var img1="";

if(window.opener.document.getElementById("imgpath1"))

{

window.opener.document.getElementById("imgpath1").value=img1;

window.opener.document.getElementById("showimg1").src="upload/"+img1;

}

window.close();

}

out.print(renderHtml("5",basePath,URLEncoder.encode(uploadtype+"@"+uid)));

%>

}else if("uploadavatar".equals(action)){//上传临时图片,可以自己实现

//System.out.println(action);

UploadUtil uploadUtil = new UploadUtil();

boolean isOk = uploadUtil.initParam(pageContext,request);

if (isOk == false)

{

System.out.println("页面出错:" + uploadUtil.getMsg());

return;

}

//上传附件

isOk = uploadUtil.uploadFile("");

if (isOk == false)

{

System.out.println("页面出错:" + uploadUtil.getMsg());

return;

}

Map map = uploadUtil.getUploadFiles();

if (map == null)

{

System.out.println("页面出错:上传附件失败");

}

if (map.size() == 0)

{

System.out.println("页面出错:没有上传附件");

return;

}

//获取附件文件名和存储文件路径

for (Iterator iter = map.keySet().iterator(); iter.hasNext();)

{

infoFilePath = (String) iter.next();

String infoFileName = (String) map.get(infoFilePath);

//infoFilePath=infoFilePath.substring(infoFilePath.lastIndexOf("\\")+1);

//System.out.println(infoFilePath);

//infoFilePath=infoFilePath.substring(infoFilePath.indexOf("webapps")+8);

infoFilePath=infoFilePath.replaceAll("\\\\", "/");

//System.out.println(basePath+"upload"+infoFilePath);

out.clear();

out.print(basePath+"upload"+infoFilePath);

}

}else if("rectavatar".equals(action)){//缩略图

String avatar1 = request.getParameter("avatar1");//大

String avatar2 = request.getParameter("avatar2");//中

String avatar3 = request.getParameter("avatar3");//小

String output ="";

try {

output = URLDecoder.decode(request.getParameter("input"));

}catch(Exception e)

{

System.out.println("解码错误!");

}

String[] tmp_input=output.split("@");//input传递的类型和uid

String pathff = request.getSession().getServletContext().getRealPath("/")+ "upload\\";

String imgfilepath=pathff+tmp_input[0];

imagepath1=imgfilepath+"/"+tmp_input[1]+"_big.jpg";

out.clear();

boolean a1=saveFile(imagepath1,getFlashDataDecode(avatar1));

if(a1){

out.print("<?xml version=\"1.0\" ?>");

}else{

out.print("<?xml version=\"1.0\" ?>");

}

}

%>

d13d3c0d53bf8f7a190cc72abfff724f.png

a5e401916f145d22dadeda93e24d845f.png

本文参考UCenter中上传头像功能的剥离php版本,陆续实现了java版本和.net版本的。 具体介绍如下(以php为例,其他版本见源码) 本文的内容仅供技术交流学习之用,相关的代码并未考虑实际应用系统中所必需的用户身份验证等功能,同时由于 UCenter 发行许可的限制,请勿将本文附带的代码直接用于实际的产品或项目中。 Discuz! 和 UCHome 中的头像上传功能很好用,大概有不少人和我一样想弄清楚它是怎么实现的,甚至希望移植到自己的应用系统中。 这个功能其实是在 UCenter 中实现的(这是 Discuz! 和 UCHome 等其它相关产品都依赖的公共模块),通过一个 Flash 文件(camera.swf),跟服务端的 PHP 程序配合完成的。 UCenter/UCHome 本身是开源的,但那个 camera.swf 并没有提供源代码。我们可以通过对 PHP 程序进行分析研究,进而基本摸清这个功能操作的细节,并最终将其剥离出来,独立于 UCenter 而独立运行。本文内容依据的是 UCenter1.5.0/UCHome2.0 。 首先我们来看一下 UCenter/UCHome 中这个功能是怎么工作的。 ■ 基本步骤 0. 浏览器访问 UCHome 中的一个 web 页面,其中包含 camera.swf。为保证 camera.swf 能正常工作,在其相同的路径下需要有 locale.xml 文件。 装载 camera.swf 的 HTML 代码可以由下面的程序生成: home\uc_client\client.php : uc_avatar() 生成的内容大致如下: <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="447" height="477" id="mycamera" align="middle"> <param name="allowScriptAccess" value="always" /> <param name="scale" value="exactfit" /> <param name="wmode" value="transparent" /> <param name="quality" value="high" /> <param name="bgcolor" value="#ffffff" /> <param name="movie" value="http://localhost/center/images/camera.swf?inajax=1&appid=1&input=6b49vKhr%2FC4VpMIMCAt07Kr2eQk8jjY%2F6vtvDonod47dU7JK%2BxVFJPIVY%2FJGMQoSpzHylapBa8FbsEWFGorPwZDJRM10wGjxvbHKTVmVgrng%2BpoSTTsk%2BE3U&agent=a523e70c80e13e4eaee37c7f4bde4f2c&ucapi=http%3A%2F%2Flocalhost%2Fcenter&avatartype=virtual" /> <param name="menu" value="false" /> <embed src="http://localhost/center/images/camera.swf?inajax=1&appid=1&input=6b49vKhr%2FC4VpMIMCAt07Kr2eQk8jjY%2F6vtvDonod47dU7JK%2BxVFJPIVY%2FJGMQoSpzHylapBa8FbsEWFGorPwZDJRM10wGjxvbHKTVmVgrng%2BpoSTTsk%2BE3U&agent=a523e70c80e13e4eaee37c7f4bde4f2c&ucapi=http%3A%2F%2Flocalhost%2Fcenter&avatartype=virtual" quality="high" bgcolor="#ffffff" width="447" height="477" name="mycamera" align="middle" allowScriptAccess="always" allowFullScreen="false" scale="exactfit" wmode="transparent" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /> </object> 参数分解如下: inajax 1 appid 1 input 6b49vKhr%2FC4VpMIMCAt07Kr2eQk8jjY%2F6vtvDonod47dU7JK%2BxVFJPIVY%2FJGMQoSpzHylapBa8FbsEWFGorPwZDJRM10wGjxvbHKTVmVgrng%2BpoSTTsk%2BE3U agent a523e70c80e13e4eaee37c7f4bde4f2c ucapi http://localhost/center avatartype virtual 其中的关键参数为 ucapi,后面两步 POST 的 URL 为 <ucapi>/index.php 这里的 "/index.php" 似乎是 hardcode 在 camera.swf 里的,不过在最后的程序代码中会看到,如果我们指定的 ucapi 明确给定了一个 php 文件的话,可以绕开 index.php 这个文件名。换句话说,如果服务端不是采用 PHP,而是另外一种编程语言的话(比如 Java/.NET 等),也是可以实现的。 1. 用户在 camera.swf 中“选择图片”后,camera.swf 会立刻把选中的图片上传到服务器。 $_SERVER REQUEST_URI /center/index.php?m=user&inajax=1&a=uploadavatar&appid=1&input=ca68o1L41a1HkbmhMJVGHu0oFjxYXwadnUT9HnFpnyz5AgiZcsVGecPP38w%2BwM7XtT79zC5WMWlH8T7LvluCSXS3YUsrcJDoD1ySILNO2xo63hmBAvXVX24f&agent=a523e70c80e13e4eaee37c7f4bde4f2c&avatartype=virtual METHOD POST $_GET m user inajax 1 a uploadavatar appid 1 input ca68o1L41a1HkbmhMJVGHu0oFjxYXwadnUT9HnFpnyz5AgiZcsVGecPP38w+wM7XtT79zC5WMWlH8T7LvluCSXS3YUsrcJDoD1ySILNO2xo63hmBAvXVX24f agent a523e70c80e13e4eaee37c7f4bde4f2c avatartype virtual $_POST Filename something.jpg Upload Submit Query $_FILES 图片文件内容 处理程序为: center\control\user.php : onuploadavatar() 程序返回的内容为保存该文件的临时位置,比如: http://localhost/center/data/tmp/upload5.jpg camera.swf 会通过上面这个网址来读取图片,显示供操作。还可以有以下返回值作为错误代码: -1 : Invalid identity! -2 : Invalid photograph! -3 : No photograph be upload! -4 : Can not write to the data/tmp folder! -5 : Server can not upload! 2. 用户在 camera.swf 中“确定”后,camera.swf 会向服务器上传 3 个图片数据,作为大、中、小尺寸的头像图片。 $_SERVER REQUEST_URI /center/index.php?m=user&inajax=1&a=rectavatar&appid=1&input=ca68o1L41a1HkbmhMJVGHu0oFjxYXwadnUT9HnFpnyz5AgiZcsVGecPP38w%2BwM7XtT79zC5WMWlH8T7LvluCSXS3YUsrcJDoD1ySILNO2xo63hmBAvXVX24f&agent=a523e70c80e13e4eaee37c7f4bde4f2c&avatartype=virtual&randomnumber=5478 METHOD POST $_GET m user inajax 1 a rectavatar appid 1 input ca68o1L41a1HkbmhMJVGHu0oFjxYXwadnUT9HnFpnyz5AgiZcsVGecPP38w+wM7XtT79zC5WMWlH8T7LvluCSXS3YUsrcJDoD1ySILNO2xo63hmBAvXVX24f agent a523e70c80e13e4eaee37c7f4bde4f2c avatartype virtual randomnumber 5478 $_POST avatar1 ...<big> avatar3 ...<middle> avatar2 ...<small> 处理程序为: center\control\user.php : onrectavatar() 程序的返回内容为: 成功时: <?xml version="1.0" ?><root><face success="1"/></root> 失败时: <root><message type="error" value="-1" /></root> 3. 上传完成后,如果 camera.swf 能找到名为 updateavatar() 的 Javascript function,就会调用它。 ■ 参数分析 在上面第 0 步生成的 HTML 中,指向 camera.swf 的 URL 携带了一些参数,其中最关键的是 ucapi,它决定了第 1 步和第 2 步的 HTTP Request 发往哪里。 其它几个都是 UCenter 应用系统所需要的参数。camera.swf 是专为 UCenter 设计的,所以它特别支持这几个参数值,也就是说,在访问 camera.swf 的 URL 里包含了什么值,在后续访问 ucapi 的时候就会原样传回来。 camera.swf 没有提供源代码,没办法改造,但并不妨碍我们使用它。比如,我们可以借用 input 这个参数,把自己应用程序所需要的自定义参数(比如 user id)统一编码装在这个值里。 ■ 剥离实现 搞清楚了功能操作的细节和参数含义,重新实现这个功能就很容易了。 所谓重新剥离实现,就是在直接借用 camera.swf/locale.xml 这两个文件的前提下,重新编写服务器端的处理程序,实现头像上传功能,脱离 UCenter 独立运行。 理论上任何服务端编程语言都可以,我这里只给出 PHP 的代码。 包含了三个程序文件: avatar.php -- 实现了上面“基本步骤”中提到的全部功能 camera.swf -- 提取自 UCenter locale.xml -- 提取自 UCenter 将这三个文件部署到 web server 上,然后访问 http://localhost/avatar.php 即可。还可以在 URL 上指定一个 uid 作为“用户标识”,比如 http://localhost/avatar.php?uid=5
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值