flash 在线视频拍照并且保存图片
下面几段代码列出了as 2.0 as 3.0的摄像头拍照功能的实现代码,最后一个由as 3.0 +jsp的实现在线拍照片后再把拍得的照片进行保存到本地或数据的代码。
方法一 as 2.0
cam= Camera.get(); //抓取当前默认的摄像头图像
MyCam.attachVideo(cam);
方法二 as 2.0
var my_video:Video;
my_video.clear()
my_video._x=100;
my_video._y=40;
var my_cam:Camera = Camera.get();
my_cam.setMode(180, 140, 10, true);
my_video.attachVideo(my_cam);
shutter.onRelease = function() {
myBitmap = new flash.display.BitmapData(160, 120, true, 0);
myBitmap.draw(my_video);
var tempObj = _root.createEmptyMovieClip("photo", 100);
tempObj._x = 300;
tempObj._y = 40;
tempObj._width= 180;
tempObj._height = 240;
tempObj.attachBitmap(myBitmap, 1, "always", true);
};
方法三 as 3.0
myCam = Camera.get();
myCam.setMode(266,200,10);
my_video.attachVideo(myCam);
3 ,以下代码实现图片的获得和上传
import flash.display.BitmapData;
import flash.geom.Rectangle;
import flash.geom.ColorTransform;
class PrintScreen {
public var addListener:Function
public var broadcastMessage:Function
private var id: Number;
public var record:LoadVars;
function PrintScreen(){
AsBroadcaster.initialize( this );
}
public function print(mc:MovieClip, w:Number, h:Number){
broadcastMessage("onStart", mc);
w = Math.floor(w);
h = Math.floor(h);
var bmp:BitmapData = new BitmapData(w, h, false);
record = new LoadVars();
record.width = w
record.height = h
record.cols = 0
record.rows = 0
bmp.draw(mc);
id = setInterval(copysource, 1, this, mc, bmp);
}
private function copysource(scope, movie, bit){
var pixel:Number
var str_pixel:String
scope.record["px" + scope.record.rows] = new Array();
for(var a = 0; a < bit.width; a++){
pixel = bit.getPixel(a, scope.record.rows )
str_pixel = pixel.toString(16)
scope.record["px" + scope.record.rows].push(str_pixel)
}
scope.record.rows += 1
if(scope.record.rows >= bit.height){
clearInterval(scope.id)
bit.dispose();
}
}
}
最后再加上一个拍照后再保存到服务器吧。jsp版式
String width = request.getParameter("width");
String height = request.getParameter("height");
int w = Integer.parseInt(width);
int h = Integer.parseInt(height);
try {
OutputStream fos = response.getOutputStream();
BufferedImage bf = new BufferedImage(w, h,
BufferedImage.TYPE_INT_RGB);
for (int i = 0; i < bf.getHeight(); i++) {
String data = request.getParameter("px" + i);
String[] ds = data.split(",");
for (int j = 0; j < bf.getWidth(); j++) {
int d = Integer.parseInt(ds[j], 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 null;
}
IIOImage iioImage = new IIOImage(bf, null, null);
ImageWriteParam param = writer.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality(0.2f);
// save to file
ImageOutputStream outputStream2 = ImageIO
.createImageOutputStream(response.getOutputStream());
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 null;
补充:flash教程,As3.0