使用base64 对图像进行 转换的小程序。附上对视频进行截图的功能程序。
java端,对jpeg图像转码成 base64字串
- /*
- * Created on 2008-9-25
- *
- * 徐泽宇 roamer
- */
- package tester;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import sun.misc.BASE64Decoder;
- import sun.misc.BASE64Encoder;
- public class Image2Base64
- {
- public static void main(String[] args)
- {
- String strImg = GetImageStr();
- GenerateImage(strImg);
- }
- public static String GetImageStr()
- {//将图片文件转化为字节数组字符串,并对其进行Base64编码处理
- String imgFile = "d://1.jpg";//待处理的图片
- InputStream in = null;
- byte[] data = null;
- //读取图片字节数组
- try
- {
- in = new FileInputStream(imgFile);
- data = new byte[in.available()];
- in.read(data);
- in.close();
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- //对字节数组Base64编码
- BASE64Encoder encoder = new BASE64Encoder();
- System.out.print(encoder.encode(data));
- return encoder.encode(data);//返回Base64编码过的字节数组字符串
- }
- public static boolean GenerateImage(String imgStr)
- {//对字节数组字符串进行Base64解码并生成图片
- if (imgStr == null) //图像数据为空
- return false;
- BASE64Decoder decoder = new BASE64Decoder();
- try
- {
- //Base64解码
- byte[] b = decoder.decodeBuffer(imgStr);
- for(int i=0;i<b.length;++i)
- {
- if(b[i]<0)
- {//调整异常数据
- b[i]+=256;
- }
- }
- //生成jpeg图片
- String imgFilePath = "d://2.jpg";//新生成的图片
- OutputStream out = new FileOutputStream(imgFilePath);
- out.write(b);
- out.flush();
- out.close();
- return true;
- }
- catch (Exception e)
- {
- return false;
- }
- }
- }
flex 这端对base64编码 转换成图片
- <?xml version="1.0" encoding="utf-8"?>
- <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
- layout="vertical"
- verticalAlign="middle"
- backgroundColor="white"
- creationComplete="init();">
- <mx:Script>
- <![CDATA[
- import mx.utils.Base64Decoder;
- private var base64Dec:Base64Decoder;
- private function decode():void{
- }
- private function load():void{
- mx.controls.Alert.show(this.textArea.text);
- var byteArr:ByteArray;
- base64Dec = new Base64Decoder();
- base64Dec.decode(this.textArea.text);
- byteArr = base64Dec.toByteArray();
- img.load(byteArr);
- }
- private function init():void {
- }
- ]]>
- </mx:Script>
- <mx:Form width="100%" height="100%">
- <mx:FormItem label="image:">
- <mx:Image id="img" />
- </mx:FormItem>
- <mx:FormItem label="source:"
- width="100%"
- height="100%">
- <mx:TextArea id="textArea"
- text=""
- editable="true"
- width="100%"
- height="100%" />
- </mx:FormItem>
- </mx:Form>
- <mx:Button label="显示" click="load()">
- </mx:Button>
- </mx:Application>
- <?xml version="1.0" encoding="utf-8"?>
- <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="388" height="454" creationComplete="initApp()">
- <mx:Style>
- Alert{font-size:12px;}
- </mx:Style>
- <mx:Script>
- <![CDATA[
- import mx.events.CloseEvent;
- import mx.rpc.events.FaultEvent;
- import mx.rpc.events.ResultEvent;
- import mx.controls.Alert;
- import mx.rpc.remoting.RemoteObject;
- import mx.graphics.ImageSnapshot;
- private static const DEFAULT_CAMERA_WIDTH:Number = 160; //摄像头显示宽度
- private static const DEFAULT_CAMERA_HEIGHT:Number = 120; //摄像头显示高度
- private static const DEFAULT_WEBSERVICE_URL:String = "http://localhost:1888/Web/TestWebService.asmx?WSDL"; //WebService地址
- private var m_camera:Camera; //定义一个摄像头
- private var m_localVideo:Video; //定义一个本地视频
- private var m_pictureBitmapData:BitmapData //定义视频截图
- [Bindable]
- private var m_pictureData:String;
- [Bindable]
- private var base64String:String="";
- private function initApp():void
- {
- t_btn_Shooting.enabled = false;
- t_ban_Save.enabled = false;
- initCamera();
- }
- //初始化摄像头
- private function initCamera():void
- {
- m_camera = Camera.getCamera();
- if(m_camera != null)
- {
- m_camera.addEventListener(StatusEvent.STATUS,__onCameraStatusHandler);
- m_camera.setMode(DEFAULT_CAMERA_WIDTH,DEFAULT_CAMERA_HEIGHT,30);
- m_localVideo = new Video();
- m_localVideo.width = DEFAULT_CAMERA_WIDTH;
- m_localVideo.height = DEFAULT_CAMERA_HEIGHT;
- m_localVideo.attachCamera(m_camera);
- t_vd_Video.addChild(m_localVideo);
- }
- else
- {
- Alert.show("没有找到摄像头,是否重新查找。","提示:",Alert.OK|Alert.NO,this,__InitCamera);
- return;
- }
- }
- //拍照按钮事件,进行视频截图
- private function SnapshotPicture():void
- {
- m_pictureBitmapData = new BitmapData(DEFAULT_CAMERA_WIDTH,DEFAULT_CAMERA_HEIGHT);
- m_pictureBitmapData.draw(t_vd_Video,new Matrix());
- var m_pictureBitmap:Bitmap = new Bitmap(m_pictureBitmapData);
- t_img_Picture.addChild(m_pictureBitmap);
- t_panel_Picture.visible = true;
- t_ban_Save.enabled = true;
- }
- //保存按钮事件,保存视频截图
- //通过RO保存
- private function SavePicture():void
- {
- var ohSnap:ImageSnapshot = ImageSnapshot.captureImage(this.t_img_Picture);
- this.base64String= ImageSnapshot.encodeImageAsBase64(ohSnap);
- this.picString.text =base64String;
- var ro:RemoteObject = new RemoteObject("cameraServiceRO");
- ro.addEventListener(FaultEvent.FAULT,__onSavePictureFault);
- ro.addEventListener(ResultEvent.RESULT,__onSavePictureResult);
- ro.save(this.base64String);
- }
- //检测摄像头权限事件
- private function __onCameraStatusHandler(event:StatusEvent):void
- {
- if(!m_camera.muted)
- {
- t_btn_Shooting.enabled = true;
- }
- else
- {
- Alert.show("无法链接到活动摄像头,是否重新检测。","提示:",Alert.OK|Alert.NO,this,__InitCamera);
- }
- m_camera.removeEventListener(StatusEvent.STATUS,__onCameraStatusHandler);
- }
- //当摄像头不存在,或连接不正常时重新获取
- private function __InitCamera(event:CloseEvent):void
- {
- if(event.detail == Alert.OK)
- {
- initApp();
- }
- }
- //WebService保存图片成功事件
- private function __onSavePictureResult(event:ResultEvent):void
- {
- Alert.show("ok");
- }
- //连接WebService失败事件
- private function __onSavePictureFault(event:FaultEvent):void
- {
- //Alert.show(event.fault.toString(),"提示",Alert.OK);
- Alert.show(event.fault.toString());
- }
- //保存图片成功后的弹出窗口确认事件
- private function __onAlertCloseHandler(event:CloseEvent):void
- {
- if(event.detail == Alert.OK)
- {
- //trace("转向页面");
- }
- }
- ]]>
- </mx:Script>
- <mx:Panel x="10" y="10" width="180" height="200" layout="absolute" title="视频拍照" fontSize="12">
- <mx:VideoDisplay id="t_vd_Video" width="160" height="120"/>
- <mx:ControlBar horizontalAlign="right">
- <mx:Button id="t_btn_Shooting" label="拍照" click="SnapshotPicture()"/>
- </mx:ControlBar>
- </mx:Panel>
- <mx:Panel id="t_panel_Picture" x="198" y="10" width="180" height="200" layout="absolute" title="拍照图片" fontSize="12" visible="false">
- <mx:Image id="t_img_Picture" x="0" y="0" width="160" height="120"/>
- <mx:ControlBar horizontalAlign="right">
- <mx:Button id="t_ban_Save" label="保存" click="SavePicture()" />
- </mx:ControlBar>
- </mx:Panel>
- <mx:TextArea text="{base64String}" id="picString" x="10" y="218" width="368" height="226"/>
- </mx:Application>