以前发过一个抓图的,没有实现存储,这次给完成,主要就下面些
Flash部分:因为以前发过,这次就主要发传数据的部分,看不懂得可以去搜Flash视频+抓图
//传值
var send_lv:LoadVars=new LoadVars();
var load_lv:LoadVars=new LoadVars();
load_lv.onLoad=function(success) {
trace("ok");
}
//抓图///
import flash.display.BitmapData;
var pixData:String="";
photo_btn.onRelease=function(){
var WIDTH:Number=my_video.width;
var HEIGHT:Number=my_video.height;
timer=getTimer();
var bd:BitmapData=new BitmapData(WIDTH/2.5,HEIGHT/2.5,false,0x00CCCCCC);
bd.draw(my_video);
var tempObj:MovieClip = _root.createEmptyMovieClip("tempObj", 100);
tempObj._x = 50;
tempObj._y = 25;
tempObj.attachBitmap(bd, 1, "always", true);
var space:Array=new Array("cao","00000","0000","000","00","0");
for(var i=0;i<160;i++) {
for(var j=0;j<120;j++) {
pixInfo=bd.getPixel(i,j).toString(16);
if(pixInfo.length<6){
pixInfo=space[pixInfo.length]+pixInfo;
}//endif
pixData+=pixInfo+",";//String.fromCharCode(pixInfo)+",";
}//endfor1
}//endfor2
trace(pixData.length);
}
save_btn.onRelease=function(){
send_lv.pixData=pixData;
send_lv.sendAndLoad("http://127.0.0.1:8080/test/ProcessPixServlet",load_lv,"POST");
}
Servlet:
import javax.servlet. * ;
import javax.servlet.http. * ;
public class SavePixInfo extends HttpServlet ... {
public void service(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException...{
response.setContentType("text/html;charset=GB2312");
PrintWriter out=response.getWriter();
String pixData=request.getParameter("pixData");
CreateJPEG fj=new CreateJPEG();
fj.setPixData(pixData);
fj.drawImage();
}
}
CreateJPEG.java
import java.util. * ;
import com.sun.image.codec.jpeg. * ;
import java.awt.image. * ;
import java.awt. * ;
public class CreateJPEG ... {
BufferedImage image;
private String pixData=null;
public String getPixData() ...{
return this.pixData;
}
public void setPixData(String pixData) ...{
this.pixData=pixData;
}
public void createJpg(String path) ...{
try ...{
FileOutputStream fos = new FileOutputStream(path);
BufferedOutputStream bos = new BufferedOutputStream(fos);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);
encoder.encode(image);
bos.close();
} catch(FileNotFoundException fnfe) ...{
System.out.println(fnfe);
} catch(IOException ioe) ...{
System.out.println(ioe);
}
}
public void drawImage()...{
int width=160, height=120;
CreateJPEG jg = new CreateJPEG();
jg.image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = jg.image.getGraphics();
try ...{
BufferedWriter writerBuff=new BufferedWriter(new FileWriter("d:/ww.txt"));
writerBuff.write(pixData,0,pixData.length());
writerBuff.close();
}
catch(IOException e)
...{
System.out.println(e.toString());
}
String[] pixInfo=pixData.split(",");
System.out.println(pixInfo.length);
int count=0;
for(int i=0;i<width;i++) ...{
for(int j=0;j<height;j++)...{
String pixRStr=pixInfo[count].substring(0, 2);
String pixGStr=pixInfo[count].substring(2, 4);
String pixBStr=pixInfo[count].substring(4);
int pixR=Integer.parseInt(pixRStr, 16);
int pixG=Integer.parseInt(pixGStr, 16);
int pixB=Integer.parseInt(pixBStr, 16);
// System.out.println("R:"+pixR+"=G:"+pixG+"+B:"+pixB);
g.setColor(new Color(pixR,pixG,pixB));
g.fillRect(i, j, 1, 1);
count++;
}
}
jg.createJpg("d:/myJPG.jpg");
}
}
web.xml
< web-app id ="WebApp_ID" version ="2.4" xmlns ="http://java.sun.com/xml/ns/j2ee" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation ="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >
< display-name > test </ display-name >
< servlet >
< servlet-name > ProcessPixServlet </ servlet-name >
< servlet-class > SavePixInfo </ servlet-class >
</ servlet >
< servlet-mapping >
< servlet-name > ProcessPixServlet </ servlet-name >
< url-pattern > /ProcessPixServlet </ url-pattern >
</ servlet-mapping >
</ web-app >
源文件就不发了,如果看不懂得话建议去翻翻帮助
烟头 2007-04-26