java端,对jpeg图像转码成 base64字串
- 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()
- {
- 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();
- }
-
- BASE64Encoder encoder = new BASE64Encoder();
- System.out.print(encoder.encode(data));
- return encoder.encode(data);
- }
- public static boolean GenerateImage(String imgStr)
- {
- if (imgStr == null)
- return false;
- BASE64Decoder decoder = new BASE64Decoder();
- try
- {
-
- byte[] b = decoder.decodeBuffer(imgStr);
- for(int i=0;i<b.length;++i)
- {
- if(b[i]<0)
- {
- b[i]+=256;
- }
- }
-
- 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>
对视频进行截图。然后生成base64字符串传递给RO的小程序。- <?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;
- }
-
-
-
- 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();
- }
- }
-
-
- private function __onSavePictureResult(event:ResultEvent):void
- {
- Alert.show("ok");
- }
-
-
- private function __onSavePictureFault(event:FaultEvent):void
- {
-
- Alert.show(event.fault.toString());
- }
-
-
- private function __onAlertCloseHandler(event:CloseEvent):void
- {
- if(event.detail == Alert.OK)
- {
-
- }
- }
- ]]>
- </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>