打开Flash CS3,新建一个Flash文件(Script 2.0)。命名为pagea.fla
第一步: 在库中建立一个视频元件,类型选择:视频(受 ActionScript 控制)。 按F11打开库面板,右键空白地方出现一个弹出菜单,选择“新建视频”,随便起个名字。
第二步: 将视频元件拖到场景中,并调整尺寸到想要的大小。并起名为“camera”。同时在该图层放置两个Button,分别命名为“btnCamera”和“btnUpload”。
第三步: 新建一个图层,命名为“代码层” 点击时间轴第一帧,按F9打开代码面板,粘贴如下代码:
-
-
-
-
- camera.clear();
-
-
-
-
-
- var cam:Camera = Camera.get();
-
- if ( cam == null){
-
-
-
- }
-
-
-
- photow = new Number(160);
-
- photoh = new Number(120);
-
-
-
- cam.setMode(photow, photoh, 10, true);
-
- camera.attachVideo(cam);
-
-
-
-
-
- var lv:LoadVars;
-
- lv = new LoadVars();
-
-
-
-
-
-
-
-
-
- btnCamera.onRelease = function ()
-
- {
-
-
-
- saveCamera();
-
-
-
- };
-
-
-
-
-
-
-
-
-
- function saveCamera(){
-
-
-
- lv.bm = new flash.display.BitmapData(photow, photoh,true, 0);
-
- lv.bm.draw(camera);
-
-
-
- var tempObj = _root.createEmptyMovieClip("photo", 100);
-
- tempObj._x = 300;
-
- tempObj._y = 40;
-
- tempObj.attachBitmap(lv.bm, 1);
-
- }
-
-
-
-
-
- btnUpload.onRelease = function(){
-
-
-
-
-
- i = lv.bm.height;
-
- j = lv.bm.width;
-
- rgb_array = "";
-
- for (y = i - 1; y >= 0; y--)
-
- {
-
- for (x = 0; x < j; x++)
-
- {
-
- color = lv.bm.getPixel(x, y).toString(16);
-
- rgb_array = rgb_array + (color + ",");
-
- }
-
- }
-
- lv.height = i;
-
- lv.width = j;
-
- lv.rgb_array = rgb_array;
-
-
-
-
-
-
-
- lv.sendAndLoad("pageb.jsp", lv, "POST");
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- }
第四步: 按F12把flash导出成为html,你可以得到pagea.fla,pagea.html, pagea.swf和AC_RunActiveContent.js。 把pagea.html重命名为pagea.jsp,放置到你的jsp服务器目录下
第五步: 新建一个文件叫做pageb.jsp。粘贴如下代码
- <%@page import="java.io.*,java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*,javax.imageio.stream.*"%><%
-
- String width = request.getParameter("width");
-
- String height = request.getParameter("height");
-
- int w = Integer.parseInt(width);
-
- int h = Integer.parseInt(height);
-
- try {
-
-
-
- OutputStream fos = new FileOutputStream(new File("c:\\a.jpg"));
-
- BufferedImage bf = new BufferedImage(w, h,
-
- BufferedImage.TYPE_INT_RGB);
-
-
-
- String data = request.getParameter("rgb_array");
-
- String[] ds = data.split(",");
-
- int leng=ds.length-1;
-
- for (int i = bf.getHeight()-1; i>=0 ; i--) {
-
- for (int j = 0; j < bf.getWidth(); j++) {
-
- int d = Integer.parseInt(ds[leng-(i*w+(bf.getWidth()-j-1))], 16);
-
-
-
- bf.setRGB(j, i, d);
-
- }
-
- }
-
-
-
- ImageWriter writer = null;
-
- ImageTypeSpecifier type = ImageTypeSpecifier.createFromRenderedImage(bf);
-
- Iterator iter = ImageIO.getImageWriters(type, "jpg");
-
- if (iter.hasNext()) {
-
- writer = (ImageWriter) iter.next();
-
- }
-
- if (writer == null) {
-
- return ;
-
- }
-
- IIOImage iioImage = new IIOImage(bf, null, null);
-
- ImageWriteParam param = writer.getDefaultWriteParam();
-
-
-
- param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
-
-
-
- param.setCompressionQuality(0.2f);
-
-
-
-
-
-
-
- ImageOutputStream outputStream2 = ImageIO.createImageOutputStream(fos);
-
- writer.setOutput(outputStream2);
-
- writer.write(null, iioImage, param);
-
-
-
- fos.close();
-
-
-
- } catch (Exception e) {
-
- e.printStackTrace();
-
- }
-
- System.out.println("w = " + w + ",h=" + h);
-
- response.setContentType("image/jpeg");
-
- return ;
-
-
-
-
-
- %>
此时你可以通过访问http://yourserver/pagea.jsp 来访问拍照页面,允许flash访问camera。等待画面出来之后,点击拍照和上传按钮,图片就会上传到服务器的c:\\a.jpg中。
本例仅给出简单参考,你可以根据实际需要设定图片保存的临时目录和文件名,并处理完成过后的提示信息。