加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

使用base64 对图像进行 转换的小程序。附上对视频进行截图的功能

发布时间:2020-12-17 02:38:57 所属栏目:安全 来源:网络整理
导读: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.
java端,对jpeg图像转码成 base64字串
  1. /*
  2. ?*?Created?on?2008-9-25
  3. ?*
  4. ?*?徐泽宇?roamer
  5. ?*/
  6. package?tester;
  7. import?java.io.FileInputStream;
  8. import?java.io.FileOutputStream;
  9. import?java.io.IOException;
  10. import?java.io.InputStream;
  11. import?java.io.OutputStream;
  12. import?sun.misc.BASE64Decoder;
  13. import?sun.misc.BASE64Encoder;
  14. public?class?Image2Base64?
  15. {
  16. ????public?static?void?main(String[]?args)
  17. ????{
  18. ????????String?strImg?=?GetImageStr();
  19. ????????GenerateImage(strImg);
  20. ????}
  21. ????public?static?String?GetImageStr()
  22. ????{//将图片文件转化为字节数组字符串,并对其进行Base64编码处理
  23. ????????String?imgFile?=?"d://1.jpg";//待处理的图片
  24. ????????InputStream?in?=?null;
  25. ????????byte[]?data?=?null;
  26. ????????//读取图片字节数组
  27. ????????try?
  28. ????????{
  29. ????????????in?=?new?FileInputStream(imgFile);????????
  30. ????????????data?=?new?byte[in.available()];
  31. ????????????in.read(data);
  32. ????????????in.close();
  33. ????????}?
  34. ????????catch?(IOException?e)?
  35. ????????{
  36. ????????????e.printStackTrace();
  37. ????????}
  38. ????????//对字节数组Base64编码
  39. ????????BASE64Encoder?encoder?=?new?BASE64Encoder();
  40. ????????System.out.print(encoder.encode(data));
  41. ????????return?encoder.encode(data);//返回Base64编码过的字节数组字符串
  42. ????}
  43. ????public?static?boolean?GenerateImage(String?imgStr)
  44. ????{//对字节数组字符串进行Base64解码并生成图片
  45. ????????if?(imgStr?==?null)?//图像数据为空
  46. ????????????return?false;
  47. ????????BASE64Decoder?decoder?=?new?BASE64Decoder();
  48. ????????try?
  49. ????????{
  50. ????????????//Base64解码
  51. ????????????byte[]?b?=?decoder.decodeBuffer(imgStr);
  52. ????????????for(int?i=0;i<b.length;++i)
  53. ????????????{
  54. ????????????????if(b[i]<0)
  55. ????????????????{//调整异常数据
  56. ????????????????????b[i]+=256;
  57. ????????????????}
  58. ????????????}
  59. ????????????//生成jpeg图片
  60. ????????????String?imgFilePath?=?"d://2.jpg";//新生成的图片
  61. ????????????OutputStream?out?=?new?FileOutputStream(imgFilePath);????
  62. ????????????out.write(b);
  63. ????????????out.flush();
  64. ????????????out.close();
  65. ????????????return?true;
  66. ????????}?
  67. ????????catch?(Exception?e)?
  68. ????????{
  69. ????????????return?false;
  70. ????????}
  71. ????}
  72. }
flex 这端对base64编码 转换成图片
  1. <?xml?version="1.0"?encoding="utf-8"?>
  2. <mx:Application?xmlns:mx="http://www.adobe.com/2006/mxml"
  3. ????????layout="vertical"
  4. ????????verticalAlign="middle"
  5. ????????backgroundColor="white"
  6. ????????creationComplete="init();">
  7. ?
  8. ????<mx:Script>
  9. ????????<![CDATA[
  10. ????????????import?mx.utils.Base64Decoder;
  11. ???????????
  12. ????????????private?var?base64Dec:Base64Decoder;
  13. ????????????
  14. ????????????private?function?decode():void{
  15. ????????????????
  16. ????????????}
  17. ????????????
  18. ????????????private?function?load():void{
  19. ????????????????mx.controls.Alert.show(this.textArea.text);
  20. ????????????????var?byteArr:ByteArray;
  21. ?
  22. ????????????????base64Dec?=?new?Base64Decoder();
  23. ????????????????base64Dec.decode(this.textArea.text);
  24. ?
  25. ????????????????byteArr?=?base64Dec.toByteArray();
  26. ?
  27. ????????????????img.load(byteArr);
  28. ????????????}
  29. ????????????
  30. ????????????private?function?init():void?{
  31. ????????????????
  32. ????????????}
  33. ????????]]>
  34. ????</mx:Script>
  35. ?
  36. ????
  37. ?
  38. ????<mx:Form?width="100%"?height="100%">
  39. ????????<mx:FormItem?label="image:">
  40. ????????????<mx:Image?id="img"?/>
  41. ????????</mx:FormItem>
  42. ????????<mx:FormItem?label="source:"
  43. ????????????????width="100%"
  44. ????????????????height="100%">
  45. ????????????<mx:TextArea?id="textArea"
  46. ????????????????????text=""
  47. ????????????????????editable="true"
  48. ????????????????????width="100%"
  49. ????????????????????height="100%"?/>
  50. ????????</mx:FormItem>
  51. ????</mx:Form>
  52. ????<mx:Button?label="显示"?click="load()">
  53. ????????
  54. ????</mx:Button>
  55. ?
  56. </mx:Application>
对视频进行截图。然后生成base64字符串传递给RO的小程序。
  1. <?xml?version="1.0"?encoding="utf-8"?>
  2. <mx:Application?xmlns:mx="http://www.adobe.com/2006/mxml"?layout="absolute"?width="388"?height="454"?creationComplete="initApp()">
  3. ????<mx:Style>
  4. ????????Alert{font-size:12px;}
  5. ????</mx:Style>
  6. ????<mx:Script>
  7. ????????<![CDATA[
  8. ????????????import?mx.events.CloseEvent;
  9. ????????????import?mx.rpc.events.FaultEvent;
  10. ????????????import?mx.rpc.events.ResultEvent;
  11. ????????????import?mx.controls.Alert;
  12. ????????????import?mx.rpc.remoting.RemoteObject;
  13. ????????????import?mx.graphics.ImageSnapshot;
  14. ????????????
  15. ????????????private?static?const?DEFAULT_CAMERA_WIDTH:Number?=?160;?//摄像头显示宽度
  16. ????????????private?static?const?DEFAULT_CAMERA_HEIGHT:Number?=?120;?//摄像头显示高度
  17. ????????????private?static?const?DEFAULT_WEBSERVICE_URL:String?=?"http://localhost:1888/Web/TestWebService.asmx?WSDL";?//WebService地址
  18. ????????????
  19. ????????????private?var?m_camera:Camera;?//定义一个摄像头
  20. ????????????private?var?m_localVideo:Video;?//定义一个本地视频
  21. ????????????private?var?m_pictureBitmapData:BitmapData?//定义视频截图
  22. ????????????[Bindable]
  23. ????????????private?var?m_pictureData:String;
  24. ????????????[Bindable]
  25. ????????????private?var?base64String:String="";
  26. ????????????
  27. ????????????private?function?initApp():void
  28. ????????????{
  29. ????????????????t_btn_Shooting.enabled?=?false;
  30. ????????????????t_ban_Save.enabled?=?false;
  31. ????????????????initCamera();
  32. ????????????}
  33. ????????????
  34. ????????????//初始化摄像头
  35. ????????????private?function?initCamera():void
  36. ????????????{
  37. ????????????????m_camera?=?Camera.getCamera();
  38. ????????????????if(m_camera?!=?null)
  39. ????????????????{
  40. ????????????????????m_camera.addEventListener(StatusEvent.STATUS,__onCameraStatusHandler);
  41. ????????????????????
  42. ????????????????????m_camera.setMode(DEFAULT_CAMERA_WIDTH,DEFAULT_CAMERA_HEIGHT,30);
  43. ????????????????????m_localVideo?=?new?Video();
  44. ????????????????????m_localVideo.width?=?DEFAULT_CAMERA_WIDTH;
  45. ????????????????????m_localVideo.height?=?DEFAULT_CAMERA_HEIGHT;
  46. ????????????????????m_localVideo.attachCamera(m_camera);
  47. ????????????????????t_vd_Video.addChild(m_localVideo);
  48. ????????????????}
  49. ????????????????else
  50. ????????????????{
  51. ????????????????????Alert.show("没有找到摄像头,是否重新查找。","提示:",Alert.OK|Alert.NO,this,__InitCamera);
  52. ????????????????????return;
  53. ????????????????}
  54. ????????????}
  55. ????????????
  56. ????????????//拍照按钮事件,进行视频截图
  57. ????????????private?function?SnapshotPicture():void
  58. ????????????{
  59. ????????????????m_pictureBitmapData?=?new?BitmapData(DEFAULT_CAMERA_WIDTH,DEFAULT_CAMERA_HEIGHT);
  60. ????????????????m_pictureBitmapData.draw(t_vd_Video,new?Matrix());
  61. ????????????????
  62. ????????????????var?m_pictureBitmap:Bitmap?=?new?Bitmap(m_pictureBitmapData);
  63. ????????????????t_img_Picture.addChild(m_pictureBitmap);
  64. ????????????????
  65. ????????????????t_panel_Picture.visible?=?true;
  66. ????????????????t_ban_Save.enabled?=?true;
  67. ????????????}
  68. ????????????
  69. ????????????//保存按钮事件,保存视频截图
  70. ????????????//通过RO保存
  71. ????????????private?function?SavePicture():void
  72. ????????????{
  73. ????????????????var?ohSnap:ImageSnapshot?=?ImageSnapshot.captureImage(this.t_img_Picture);
  74. ????????????????this.base64String=?ImageSnapshot.encodeImageAsBase64(ohSnap);
  75. ????????????????this.picString.text?=base64String;
  76. ????????????????var?ro:RemoteObject?=?new?RemoteObject("cameraServiceRO");
  77. ????????????????ro.addEventListener(FaultEvent.FAULT,__onSavePictureFault);
  78. ????????????????ro.addEventListener(ResultEvent.RESULT,__onSavePictureResult);
  79. ????????????????ro.save(this.base64String);
  80. ????????????}
  81. ????????????
  82. ????????????//检测摄像头权限事件
  83. ????????????private?function?__onCameraStatusHandler(event:StatusEvent):void
  84. ????????????{
  85. ????????????????if(!m_camera.muted)
  86. ????????????????{
  87. ????????????????????t_btn_Shooting.enabled?=?true;
  88. ????????????????}
  89. ????????????????else
  90. ????????????????{
  91. ????????????????????Alert.show("无法链接到活动摄像头,是否重新检测。",__InitCamera);
  92. ????????????????}
  93. ????????????????m_camera.removeEventListener(StatusEvent.STATUS,__onCameraStatusHandler);
  94. ????????????}
  95. ????????????
  96. ????????????//当摄像头不存在,或连接不正常时重新获取
  97. ????????????private?function?__InitCamera(event:CloseEvent):void
  98. ????????????{
  99. ????????????????if(event.detail?==?Alert.OK)
  100. ????????????????{
  101. ????????????????????initApp();
  102. ????????????????}
  103. ????????????}
  104. ????????????
  105. ????????????//WebService保存图片成功事件
  106. ????????????private?function?__onSavePictureResult(event:ResultEvent):void
  107. ????????????{
  108. ????????????????????Alert.show("ok");
  109. ????????????}
  110. ????????????
  111. ????????????//连接WebService失败事件
  112. ????????????private?function?__onSavePictureFault(event:FaultEvent):void
  113. ????????????{
  114. ????????????????//Alert.show(event.fault.toString(),"提示",Alert.OK);
  115. ????????????????Alert.show(event.fault.toString());
  116. ????????????}
  117. ????????????
  118. ????????????//保存图片成功后的弹出窗口确认事件
  119. ????????????private?function?__onAlertCloseHandler(event:CloseEvent):void
  120. ????????????{
  121. ????????????????if(event.detail?==?Alert.OK)
  122. ????????????????{
  123. ????????????????????//trace("转向页面");
  124. ????????????????}
  125. ????????????}
  126. ????????]]>
  127. ????</mx:Script>
  128. ????<mx:Panel?x="10"?y="10"?width="180"?height="200"?layout="absolute"?title="视频拍照"?fontSize="12">
  129. ????????<mx:VideoDisplay?id="t_vd_Video"?width="160"?height="120"/>
  130. ????????<mx:ControlBar?horizontalAlign="right">
  131. ????????????<mx:Button?id="t_btn_Shooting"?label="拍照"?click="SnapshotPicture()"/>
  132. ????????</mx:ControlBar>
  133. ????</mx:Panel>
  134. ????<mx:Panel?id="t_panel_Picture"?x="198"?y="10"?width="180"?height="200"?layout="absolute"?title="拍照图片"?fontSize="12"?visible="false">
  135. ????????<mx:Image?id="t_img_Picture"?x="0"?y="0"?width="160"?height="120"/>
  136. ????????<mx:ControlBar??horizontalAlign="right">
  137. ????????????<mx:Button?id="t_ban_Save"?label="保存"?click="SavePicture()"?/>
  138. ????????</mx:ControlBar>
  139. ????</mx:Panel>
  140. ????<mx:TextArea?text="{base64String}"??id="picString"?x="10"?y="218"?width="368"?height="226"/>
  141. </mx:Application>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读