• App调用Android设备本地相机拍照并保存到本地相册


    cameraBtn.addEventListener(MouseEvent.CLICK,cameraClickHandler);

    protected function cameraClickHandler(event:MouseEvent):void
    {
    // TODO Auto-generated method stub
    //先绘制当前图行到一个bigmapdata对象
    var bmpData:BitmapData = new BitmapData(GameModel.getInstance().screenWidth,GameModel.getInstance().screenHeight);
    bmpData.draw(this);

    if(CameraRoll.supportsAddBitmapData)
    {
    //测试环境是平板是会走到这里
    // creatTxt();
    var roll:CameraRoll = new CameraRoll();
    roll.addEventListener(Event.COMPLETE,onCreatePicComlete);
    roll.addEventListener(ErrorEvent.ERROR, onCreatePicError);
    roll.addBitmapData(bmpData);
    }
    else
    {
    //测试环境是虚拟机里是会走到这里

    //利用pngEncoder解码器开始对bitmapdata编码成二进制数据ByteArray
    var bytes:ByteArray = PNGEncoder.encode(bmpData);

    //把二进制数据写入到本地中

    //根据当前系统时间来编辑图片文件的名字
    var now:Date = new Date();
    var year:Number=now.getFullYear();//年
    var month:Number=now.getMonth()+1;//月
    var day:Number=now.getDate();//日

    var hour:Number = now.getHours();//时
    var min:Number = now.getMinutes();//分
    var sec:Number = now.getSeconds();//秒

    var fileName:String = "word_" + year + month + day + hour + min + sec;
    var fileFullName:String = File.applicationStorageDirectory.resolvePath("img/"+ fileName +".png").nativePath;
    var file:File = new File(fileFullName);
    trace(file.nativePath);
    var stream:FileStream = new FileStream();
    stream.open(file,FileMode.WRITE);
    stream.writeBytes(bytes);
    stream.close();
    }
    }

    protected function onCreatePicError(event:ErrorEvent):void
    {
    // TODO Auto-generated method stub
    trace("相册图片创建失败");
    // creatTxt();
    var roll:CameraRoll = CameraRoll(event.target);
    roll.removeEventListener(Event.COMPLETE,onCreatePicComlete);
    roll.removeEventListener(ErrorEvent.ERROR, onCreatePicError);
    }

    protected function onCreatePicComlete(event:Event):void
    {
    // TODO Auto-generated method stub
    trace("相册图片创建成功");
    var roll:CameraRoll = CameraRoll(event.target);
    roll.removeEventListener(Event.COMPLETE,onCreatePicComlete);
    roll.removeEventListener(ErrorEvent.ERROR, onCreatePicError);

    creatTxt();
    ZoeDelay.Delay(1500,remoeTxtHanler);
    }

    private function creatTxt():void
    {
    savePicREsultTxt = new TextField();
    var format:TextFormat = new TextFormat();
    format.size = 60;
    format.align = TextFormatAlign.CENTER;
    format.color = 0x000000;
    format.bold = true;
    savePicREsultTxt.selectable = false;
    savePicREsultTxt.mouseEnabled = false;
    savePicREsultTxt.text = "圖片保存成功!";
    // savePicREsultTxt.text = "創建圖片!";
    savePicREsultTxt.width = 500;
    savePicREsultTxt.height = 80;

    savePicREsultTxt.setTextFormat(format);
    savePicREsultTxt.x = (GameModel.getInstance().screenWidth - savePicREsultTxt.width)/2;
    savePicREsultTxt.y = (GameModel.getInstance().screenHeight - savePicREsultTxt.height)/2;
    addChild(savePicREsultTxt);
    }

    private var savePicREsultTxt:TextField;

    private function remoeTxtHanler():void
    {
    // TODO Auto Generated method stub
    this.removeChild(savePicREsultTxt);
    savePicREsultTxt = null;
    }

    如果要通过App直接访问手机的本地相册,roll.browseForImage();就解决了

  • 相关阅读:
    android studio 开发环境配置
    解决 php-cgi 启动时提示缺少 msvcr110.dll 的问题
    PHP5.5 + IIS + Win7的配置
    PHP版本VC6和VC9、Non Thread Safe和Thread Safe的区别
    [6]Telerik TreeView 复选框
    详解.NET IL代码(一)
    第三章 续:时间控件(TimePicker)
    第二章 时间控件(DateTime Picker)
    jquery常用方法
    RESTClient
  • 原文地址:https://www.cnblogs.com/kuailezoe/p/3105464.html
Copyright © 2020-2023  润新知