欢迎访问 生活随笔!

凯发k8官方网

当前位置: 凯发k8官方网 > 编程语言 > php >内容正文

php

flex php截图demo -凯发k8官方网

发布时间:2025/1/21 php 17 豆豆
凯发k8官方网 收集整理的这篇文章主要介绍了 flex php截图demo 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

在flex中使用了两种方案来处理图片:

一、直接将bytearray转为bitmap通过loader(flash.display.loader)显示在舞台上;

二、将bytearray调用将三方方法(base64),做为字符串传给php,php使用base64_decode再将图片进行保存

demo效果图:

 

操作步骤:

1、点击“载入图片”,然后点击“截取图片”,在容器中拉出一条线,之后将三确定一个红色矩形框

2、点击“保存图片”,提交后台php,并在舞台中生成一张当前截图

3、后台保存的文件格式为当前“年月日_时分秒_img.png”

 

flex代码:

1: "1.0" encoding="utf-8"?> 2: "http://www.adobe.com/2006/mxml" layout="absolute" minwidth="955" minheight="600" creationcomplete="init();"> 3: 4: 5: 6: import com.dynamicflash.util.base64; 7: 8: import mx.controls.alert; 9: import mx.core.uicomponent; 10: import mx.graphics.codec.jpegencoder; 11: import mx.graphics.codec.pngencoder; 12: import mx.utils.base64decoder; 13: import mx.utils.base64encoder; 14: 15: private var uicompt:uicomponent; 16: private var line:sprite; 17: private var line1:sprite; 18: private var line2:sprite; 19: private var line3:sprite; 20: private var line4:sprite; 21: private var pointtl:point; 22: private var pointbr:point; 23: 24: private function init():void { 25: 26: cutbtn.enabled = savebtn.enabled = false; 27: 28: loadimgbtn.addeventlistener(mouseevent.click, loadimghandler); 29: cutbtn.addeventlistener(mouseevent.click, cutimghandler); 30: savebtn.addeventlistener(mouseevent.click, saveimghandler); 31: 32: } 33: 34: private function loadimghandler(evt:mouseevent):void { 35: img.source = "./assets/water lilies.jpg"; 36: 37: loadimgbtn.enabled = savebtn.enabled = false; 38: cutbtn.enabled = true; 39: 40: uicompt = new uicomponent(); 41: 42: this.addchild(uicompt); 43: 44: line = new sprite(); 45: line1 = new sprite(); 46: line2 = new sprite(); 47: line3 = new sprite(); 48: line4 = new sprite(); 49: 50: uicompt.addchild(line); 51: uicompt.addchild(line1); 52: uicompt.addchild(line2); 53: uicompt.addchild(line3); 54: uicompt.addchild(line4); 55: } 56: 57: private function cutimghandler(evt:mouseevent):void { 58: this.addeventlistener(mouseevent.mouse_down, onmousedownhandler); 59: 60: cutbtn.enabled = false; 61: } 62: 63: private function saveimghandler(evt:mouseevent):void { 64: var w:number = math.abs(pointbr.x-pointtl.x); 65: var h:number = math.abs(pointbr.y-pointtl.y); 66: 67: var bitmapdata:bitmapdata = new bitmapdata(application.application.width, application.application.height); 68: bitmapdata.draw(canvas); 69: 70: 71: var rectangle:rectangle = new rectangle(pointtl.x,pointtl.y,w,h); 72: var quality:number = 100; 73: var jpgencode = new jpegencoder(quality); 74: var bytes:bytearray = jpgencode.encodebytearray(bitmapdata.getpixels(rectangle), w, h); 75: 76: 77: //提交至后台php保存该图片文件 78: var encoded:string = base64.encodebytearray(bytes); 79: var variables:urlvariables = new urlvariables(); 80: variables.png = encoded; 81: 82: var req:urlrequest = new urlrequest(); 83: req.method = urlrequestmethod.post; 84: req.url = "http://meteoric.com/test/uploadfile/prova2.php"; 85: req.data = variables; 86: 87: var urlloader:urlloader = new urlloader(); 88: urlloader.dataformat = urlloaderdataformat.binary; 89: urlloader.addeventlistener(event.complete, function(evt:event) { 90: urlloader.removeeventlistener(event.complete, arguments.callee); 91: urlloader = null; 92: 93: var _loader:urlloader = urlloader(evt.target); 94: alert.show(_loader.data); 95: }); 96: urlloader.load(req); 97: 98: 99: 100: 101: //将bytearray转化为bitmap 102: var loader:loader = new loader(); 103: loader.contentloaderinfo.addeventlistener(event.complete, function(evt:event) { 104: var decodedbitmapdata:bitmapdata = bitmap(evt.target.content).bitmapdata; 105: loader.x = 800; 106: loader.y = 400; 107: stage.addchild(loader); 108: loader.contentloaderinfo.removeeventlistener(event.complete, arguments.callee); 109: loader = null; 110: }); 111: loader.loadbytes(bytes); 112: } 113: 114: private function onmousedownhandler(evt:mouseevent):void { 115: pointtl = new point(mousex, mousey); 116: 117: this.addeventlistener(mouseevent.mouse_move, onmousemovehandler); 118: this.addeventlistener(mouseevent.mouse_up, onmouseuphandler); 119: } 120: 121: private function onmouseuphandler(evt:mouseevent):void { 122: this.removeeventlistener(mouseevent.mouse_move, onmousemovehandler); 123: this.removeeventlistener(mouseevent.mouse_up, onmouseuphandler); 124: this.removeeventlistener(mouseevent.mouse_down, onmousedownhandler); 125: 126: pointbr = new point(mousex, mousey); 127: line1.graphics.clear(); 128: line2.graphics.clear(); 129: line3.graphics.clear(); 130: line4.graphics.clear(); 131: line.graphics.clear(); 132: 133: var bordercolor:number = 0xff0000; 134: 135: line1.graphics.linestyle(1, bordercolor, 1); 136: line2.graphics.linestyle(1, bordercolor, 1); 137: line3.graphics.linestyle(1, bordercolor, 1); 138: line4.graphics.linestyle(1, bordercolor, 1); 139: 140: line1.graphics.moveto(pointtl.x,pointtl.y); 141: line1.graphics.lineto(pointtl.x,pointbr.y); 142: 143: 144: line2.graphics.moveto(pointtl.x,pointtl.y); 145: line2.graphics.lineto(pointbr.x,pointtl.y); 146: 147: line3.graphics.moveto(pointtl.x,pointbr.y); 148: line3.graphics.lineto(pointbr.x,pointbr.y); 149: 150: line4.graphics.moveto(pointbr.x,pointtl.y); 151: line4.graphics.lineto(pointbr.x,pointbr.y); 152: 153: savebtn.enabled = true; 154: } 155: 156: private function onmousemovehandler(evt:mouseevent):void { 157: line.graphics.clear(); 158: line.graphics.linestyle(1, 0x000000, 1); 159: line.graphics.moveto(pointtl.x, pointtl.y); 160: line.graphics.lineto(mousex, mousey); 161: } 162: 163: ]]> 164: 165: 166: "20" y="20" width="800" height="400" 167: id="canvas" 168: bordercolor="#2e2e2e" borderstyle="solid" borderthickness="1" 169: verticalscrollpolicy="off" horizontalscrollpolicy="off"> 170: "img" scalecontent="false"/> 171: 172: 173: "载入图片" x="830" y="20" id="loadimgbtn"/> 174: "截取图片" x="830" y="50" id="cutbtn"/> 175: "保存图片" x="830" y="80" id="savebtn"/> 176: 177:

引用的base64.as(存放于com.dynamicflash.util包中)

1: /* 2: base64 - 1.1.0 3: 4: 凯发k8官方网 copyright (c) 2006 steve webster 5: 6: permission is hereby granted, free of charge, to any person obtaining a copy of 7: this software and associated documentation files (the "software"), to deal in 8: the software without restriction, including without limitation the rights to 9: use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10: the software, and to permit persons to whom the software is furnished to do so, 11: subject to the following conditions: 12: 13: the above 凯发k8官方网 copyright notice and this permission notice shall be included in all 14: copies or substantial portions of the software. 15: 16: the software is provided "as is", without warranty of any kind, express or 17: implied, including but not limited to the warranties of merchantability, fitness 18: for a particular purpose and noninfringement. in no event shall the authors or 19: 凯发k8官方网 copyright holders be liable for any claim, damages or other liability, whether 20: in an action of contract, tort or otherwise, arising from, out of or in 21: connection with the software or the use or other dealings in the software. 22: */ 23:  24: package com.dynamicflash.util { 25:  26: import flash.utils.bytearray; 27: 28: public class base64 { 29: 30: private static const base64_chars:string = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789 /="; 31:  32: public static const version:string = "1.0.0"; 33:  34: public static function encode(data:string):string { 35: // convert string to bytearray 36: var bytes:bytearray = new bytearray(); 37: bytes.writeutfbytes(data); 38: 39: // return encoded bytearray 40: return encodebytearray(bytes); 41: } 42: 43: public static function encodebytearray(data:bytearray):string { 44: // initialise output 45: var output:string = ""; 46: 47: // create data and output buffers 48: var databuffer:array; 49: var outputbuffer:array = new array(4); 50: 51: // rewind bytearray 52: data.position = 0; 53: 54: // while there are still bytes to be processed 55: while (data.bytesavailable > 0) { 56: // create new data buffer and populate next 3 bytes from data 57: databuffer = new array(); 58: for (var i:uint = 0; i < 3 && data.bytesavailable > 0; i ) { 59: databuffer[i] = data.readunsignedbyte(); 60: } 61: 62: // convert to data buffer base64 character positions and 63: // store in output buffer 64: outputbuffer[0] = (databuffer[0] & 0xfc) >> 2; 65: outputbuffer[1] = ((databuffer[0] & 0x03) << 4) | ((databuffer[1]) >> 4); 66: outputbuffer[2] = ((databuffer[1] & 0x0f) << 2) | ((databuffer[2]) >> 6); 67: outputbuffer[3] = databuffer[2] & 0x3f; 68: 69: // if data buffer was short (i.e not 3 characters) then set 70: // end character indexes in data buffer to index of '=' symbol. 71: // this is necessary because base64 data is always a multiple of 72: // 4 bytes and is basses with '=' symbols. 73: for (var j:uint = databuffer.length; j < 3; j ) { 74: outputbuffer[j 1] = 64; 75: } 76: 77: // loop through output buffer and add base64 characters to 78: // encoded data string for each character. 79: for (var k:uint = 0; k < outputbuffer.length; k ) { 80: output = base64_chars.charat(outputbuffer[k]); 81: } 82: } 83: 84: // return encoded data 85: return output; 86: } 87: 88: public static function decode(data:string):string { 89: // decode data to bytearray 90: var bytes:bytearray = decodetobytearray(data); 91: 92: // convert to string and return 93: return bytes.readutfbytes(bytes.length); 94: } 95: 96: public static function decodetobytearray(data:string):bytearray { 97: // initialise output bytearray for decoded data 98: var output:bytearray = new bytearray(); 99: 100: // create data and output buffers 101: var databuffer:array = new array(4); 102: var outputbuffer:array = new array(3); 103:  104: // while there are data bytes left to be processed 105: for (var i:uint = 0; i < data.length; i = 4) { 106: // populate data buffer with position of base64 characters for 107: // next 4 bytes from encoded data 108: for (var j:uint = 0; j < 4 && i j < data.length; j ) { 109: databuffer[j] = base64_chars.indexof(data.charat(i j)); 110: } 111: 112: // decode data buffer back into bytes 113: outputbuffer[0] = (databuffer[0] << 2) ((databuffer[1] & 0x30) >> 4); 114: outputbuffer[1] = ((databuffer[1] & 0x0f) << 4) ((databuffer[2] & 0x3c) >> 2); 115: outputbuffer[2] = ((databuffer[2] & 0x03) << 6) databuffer[3]; 116: 117: // add all non-padded bytes in output buffer to decoded data 118: for (var k:uint = 0; k < outputbuffer.length; k ) { 119: if (databuffer[k 1] == 64) break; 120: output.writebyte(outputbuffer[k]); 121: } 122: } 123: 124: // rewind decoded data bytearray 125: output.position = 0; 126: 127: // return decoded data 128: return output; 129: } 130: 131: public function base64() { 132: throw new error("base64 class is static container only"); 133: } 134: } 135: }

 

php代码:

1: 2:  3: $today = date("ymd_his"); 4: $filename = $today."_img.png"; 5: $somecontent = base64_decode($_request['png']); 6:  7: if ($handle = fopen("upload/".$filename, "w ")) { 8: if (!fwrite($handle, $somecontent) == false) { 9: fclose($handle); 10: } 11: 12: echo "imageurl=".$filename; 13: } 14:  15: ?>

 

本文参考:

swf to png with actionscript 3.0 - bytearray class

利用flex 实现截图功能

转载于:https://www.cnblogs.com/meteoric_cry/archive/2011/04/06/2006884.html

总结

以上是凯发k8官方网为你收集整理的flex php截图demo的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得凯发k8官方网网站内容还不错,欢迎将凯发k8官方网推荐给好友。

  • 上一篇:
  • 下一篇:
网站地图