使用Flash AIR创建桌面无提示拍照应用示例打赏

项目关系需要创建能够拍照的桌面应用,因为比起C++、C#,Flash略为熟悉,就采用了AIR。下面是今日来测试无误的结果。

因为项目中涉及其他内容,不想暴露拍照按钮给用户,所以预留了savePic方法供需要时调用。因为是AIR应用,可以直接操作本地文件,虽然功能有限,但单纯的保存图片还是够用的。下面即为调用方法直接保存图片的简单示例,不是采用网上多数的弹出保存框形式。时间关系,不多做介绍,仅记录,有不清楚的可以留言交流。

package
{
	import flash.media.*;
	import flash.display.*;
	import flash.net.*;
	//import fl.controls.*;
	import flash.events.*;
	import flash.geom.*;
	import flash.filesystem.*;
	import com.adobe.images.JPGEncoder;
	public class PhotoSnap extends Sprite
	{
		
		private static var cam:Camera;
		private static var vid:Video;
		private static var ps:PhotoSnap;
		public static function getInstance(container:DisplayObjectContainer):PhotoSnap
		{
			if(ps==null){
				ps=new PhotoSnap(container);
			}else{
				ps.showCamera(container);
			}
			return ps;
		}
		public function PhotoSnap(container:DisplayObjectContainer):void
		{
			cam = Camera.getCamera();
			//摄像头视窗大小
			cam.setMode(800, 600, 24);
			vid = new Video(600,550);
			vid.attachCamera(cam);
			vid.smoothing=true;
			vid.x=-(container.width/2);
			vid.y=-(container.height/2);
			showCamera(container);
			//button.addEventListener(MouseEvent.CLICK, savepic);
			
		}
		private function showCamera(container:DisplayObjectContainer):void
		{
			container.addChild(vid);
		}
		public function savePic(event:MouseEvent)
		{
			var bitmapData = new BitmapData(vid.width,vid.height);
			var matrix:Matrix=new Matrix();
			//关键处,该处的值应该是由video的大小和camera设置的大小决定的.
			matrix.scale(1,1);
			bitmapData.draw(vid,matrix);
			//var file:FileReference = new FileReference();
			var jpg:JPGEncoder = new JPGEncoder(100);
			//file.save(jpg.encode(jpgSource),"b.jpg");  
			var f:FileStream=new FileStream();
			var file:File=new File(File.applicationDirectory.resolvePath('data/'+Math.random().toString().substring(2)+".jpg").nativePath);
			f.openAsync(file,FileMode.WRITE);
			f.writeBytes(jpg.encode(bitmapData));
		}  
	}
}

PS:实际应用中因为不完全是AS项目,采用Flash编程,所以没有写包名,直接默认package。AS项目中可以借鉴。另外还有一个尚未解决的问题,就是Camera.getCamrea()获取到本地摄像头后若程序不再使用此摄像头,无法释放,找了很多资料没看到相关API,不知道为什么,不过按照正常情况考虑,一般拍照程序都是关闭才释放摄像头的,对于我要做的这种程序一直运行不释放的,可能需要另寻他径了。



使用Flash AIR创建桌面无提示拍照应用示例
文章《使用Flash AIR创建桌面无提示拍照应用示例》二维码
  • 微信打赏
  • 支付宝打赏
  1. http://www.lofter.com/blog/xikjbho?act=dashboardclick_20130514_04http://www.lofter.com/blog/xikjbho?act=dashboardclick_20130514_04http://www.lofter.com/blog/xiaokjnweof?act=dashboardclick_20130514_04http://www.lofter.com/blog/xiaokjnweof?act=dashboardclick_20130514_04http://www.lofter.com/blog/aljkbflwe?act=dashboardclick_20130514_04http://www.lofter.com/blog/aljkbflwe?act=dashboardclick_20130514_04http://www.lofter.com/blog/mnaof?act=dashboardclick_20130514_04http://www.lofter.com/blog/mnaof?act=dashboardclick_20130514_04http://www.lofter.com/blog/xiaolkjnbf?act=dashboardclick_20130514_04http://www.lofter.com/blog/xiaolkjnbf?act=dashboardclick_20130514_04http://www.lofter.com/blog/xiaoxinlash?act=dashboardclick_20130514_04http://www.lofter.com/blog/xiaoxinlash?act=dashboardclick_20130514_04http://www.lofter.com/blog/xiaoxalksdf?act=dashboardclick_20130514_04http://www.lofter.com/blog/xiaoxalksdf?act=dashboardclick_20130514_04http://www.lofter.com/blog/xilkoij?act=dashboardclick_20130514_04http://www.lofter.com/blog/xilkoij?act=dashboardclick_20130514_04http://www.lofter.com/blog/alksjldasd?act=dashboardclick_20130514_04http://www.lofter.com/blog/alksjldasd?act=dashboardclick_20130514_04http://www.lofter.com/blog/lkjais?act=dashboardclick_20130514_04http://www.lofter.com/blog/lkjaishttp://blog.e-works.net.cn/671591/articles/602448.htmlhttp://blog.e-works.net.cn/671592/articles/602452.htmlhttp://blog.e-works.net.cn/671592/articles/602456.htmlhttp://blog.e-works.net.cn/671594/articles/602470.htmlhttp://blog.e-works.net.cn/671594/articles/602471.htmlhttp://blog.e-works.net.cn/671596/articles/602474.htmlhttp://blog.e-works.net.cn/671596/articles/602478.htmlhttp://blog.e-works.net.cn/671600/articles/602487.htmlhttp://blog.e-works.net.cn/671600/articles/602494.htmlhttp://blog.e-works.net.cn/671600/articles/602496.htmlhttp://blog.e-works.net.cn/671602/articles/602505.htmlhttp://blog.e-works.net.cn/671602/articles/602506.htmlhttp://blog.e-works.net.cn/671602/articles/602512.htmlhttp://blog.e-works.net.cn/671605/articles/602521.htmlhttp://blog.e-works.net.cn/671605/articles/602524.htmlhttp://blog.e-works.net.cn/671607/articles/602532.htmlhttp://blog.e-works.net.cn/671607/articles/602547.htmlhttp://blog.e-works.net.cn/671608/articles/602556.htmlhttp://blog.e-works.net.cn/671608/articles/602563.htmlhttp://blog.e-works.net.cn/671609/articles/602565.htmlhttp://blog.e-works.net.cn/671610/articles/602581.htmlhttp://blog.e-works.net.cn/671610/articles/602584.htmlhttp://blog.e-works.net.cn/671611/articles/602590.htmlhttp://blog.e-works.net.cn/671611/articles/602592.htmlhttp://blog.e-works.net.cn/671614/articles/602595.htmlhttp://blog.e-works.net.cn/671614/articles/602599.htmlhttp://blog.e-works.net.cn/671616/articles/602608.htmlhttp://blog.e-works.net.cn/671616/articles/602615.htmlhttp://blog.e-works.net.cn/671617/articles/602619.htmlhttp://blog.e-works.net.cn/671617/articles/602624.html

无觅相关文章插件,快速提升流量