前兩天看了一個flex攝像頭拍照的程序感覺不錯不過是C#
代碼的,有點(diǎn)遺憾,今天給他改成了java代碼的。
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
-
<?
xml
?
version
=
"1.0"
?
encoding
=
"utf-8"
?>
??
-
<
mx:Application
?
xmlns:mx
=
"http://www.adobe.com/2006/mxml"
?
layout
=
"absolute"
?
width
=
"750"
?
height
=
"600"
?
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;??
-
??????????????
-
?????????????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;??
-
??????????????
-
?????????????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;??
-
?????????????????}??
-
?????????????}??
-
??????????????
-
?????????????//拍照按鈕事件,進(jìn)行視頻截圖??
-
?????????????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
;??
-
?????????????}??
-
??????????????
-
?????????????//保存按鈕事件,保存視頻截圖??
-
?????????????//通過WebService保存??
-
//?????????????private?function?SavePicture():void??
-
//?????????????{??
-
//?????????????????
m_pictureData
?=?
""
;??
-
//?????????????????for(var?
i:int
?=?
0
;?i?
<
?
DEFAULT_CAMERA_WIDTH
;?i++)??
-
//?????????????????{??
-
//?????????????????????for(var?
j:int
?=?
0
;?j?
<
?
DEFAULT_CAMERA_HEIGHT
;?j++)??
-
//?????????????????????{??
-
//?????????????????????????if(m_pictureData.length?
>
?0)??
-
//?????????????????????????{??
-
//?????????????????????????????m_pictureData?+=?","?+?m_pictureBitmapData.getPixel32(i,j).toString();??
-
//?????????????????????????}??
-
//?????????????????????????else??
-
//?????????????????????????{??
-
//?????????????????????????????
m_pictureData
?=?
m_pictureBitmapData
.getPixel32(i,j).toString();??
-
//?????????????????????????}??
-
//?????????????????????}??
-
//?????????????????}??
-
//???????????????//??t_ws_SavePicture.SavePicture.send();??
-
//?????????????}??
-
??????????????
-
?????????????//檢測攝像頭權(quán)限事件??
-
?????????????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);??
-
?????????????}??
-
??????????????
-
?????????????//當(dāng)攝像頭不存在,或連接不正常時重新獲取??
-
?????????????private?function?__InitCamera(event:CloseEvent):void??
-
?????????????{??
-
?????????????????if(
event.detail
?==?Alert.OK)??
-
?????????????????{??
-
?????????????????????initApp();??
-
?????????????????}??
-
?????????????}??
-
??????????????
-
?????????????//WebService保存圖片成功事件??
-
?????????????private?function?__onSavePictureResult(event:ResultEvent):void??
-
?????????????{??
-
?????????????????//trace(event.result);??
-
?????????????????if(event.result.toString()?==?"保存成功")??
-
?????????????????{??
-
?????????????????????Alert.show(event.result.toString(),"提示",Alert.OK,this,__onAlertCloseHandler);??
-
?????????????????}??
-
?????????????????else??
-
?????????????????{??
-
?????????????????????Alert.show(event.result.toString(),"提示",Alert.OK);??
-
?????????????????}??
-
?????????????}??
-
??????????????
-
?????????????//連接WebService失敗事件??
-
?????????????private?function?__onSavePictureFault(event:FaultEvent):void??
-
?????????????{??
-
?????????????????//Alert.show(event.fault.toString(),"提示",Alert.OK);??
-
?????????????????Alert.show("連接WebService失敗。","提示",Alert.OK);??
-
?????????????}??
-
??????????????
-
?????????????//保存圖片成功后的彈出窗口確認(rèn)事件??
-
?????????????private?function?__onAlertCloseHandler(event:CloseEvent):void??
-
?????????????{??
-
?????????????????if(
event.detail
?==?Alert.OK)??
-
?????????????????{??
-
?????????????????????//trace("轉(zhuǎn)向頁面");??
-
?????????????????}??
-
?????????????}??
-
???????????????
-
???????????????
-
??????????????public?function?SavePicture(){??
-
??????????????
-
????????????????????var?
params:URLVariables
?=?
new
?URLVariables();??
-
?????????????????????
m_pictureData
?=?
""
;??
-
?????????????????????for(var?
i:int
?=?
0
;?i?
<
?
DEFAULT_CAMERA_WIDTH
;?i++)??
-
?????????????????????{??
-
?????????????????????????for(var?
j:int
?=?
0
;?j?
<
?
DEFAULT_CAMERA_HEIGHT
;?j++)??
-
?????????????????????????{??
-
?????????????????????????????if(m_pictureData.length?
>
?0)??
-
?????????????????????????????{??
-
?????????????????????????????????m_pictureData?+=?","?+?m_pictureBitmapData.getPixel32(i,j).toString();??
-
?????????????????????????????}??
-
?????????????????????????????else??
-
?????????????????????????????{??
-
?????????????????????????????????
m_pictureData
?=?
m_pictureBitmapData
.getPixel32(i,j).toString();??
-
?????????????????????????????}??
-
?????????????????????????}??
-
?????????????????????}??
-
????????????????????
params.width
?=?
DEFAULT_CAMERA_WIDTH
;????
-
????????????????????
params.height
?=?
DEFAULT_CAMERA_HEIGHT
;??
-
????????????????????
params.bitmap_data
?=?
m_pictureData
;??
-
?????????????????????
-
????????????????????
savePictureService.url
?=?
"http://localhost:8088/archive/savepicturesrv"
;??
-
????????????????????savePictureService.send(params);??????
-
??????????????????
-
????????????}??
-
?????????]]--
>
??
-
?????
</
mx:Script
>
??
-
?????
-
?????
<
mx:HTTPService
?
showBusyCursor
=
"true"
?
id
=
"savePictureService"
?
method
=
"POST"
?
resultFormat
=
"xml"
??
-
?????????
fault
=
"__onSavePictureFault(event)"
?
result
=
"__onSavePictureResult(event)"
/>
?????
-
????
<
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:Application
>
??
java 代碼:
-
public
?
void
?processRequest(HttpServletRequest?request,??
-
????????????HttpServletResponse?response)?
throws
?ServletException,?IOException?{??
-
????????response.setContentType(
"text/html;charset=UTF-8"
);??
-
????????response.setHeader(
"Pragma"
,?
"No-cache"
);??
-
????????response.setHeader(
"Cache-Control"
,?
"no-cache"
);??
-
????????response.setDateHeader(
"Expires"
,?
0
);??
-
??????????????String?bitmap_data?=?request.getParameter(
"bitmap_data"
);??
-
??????????
-
????????
int
?width?=?commons.getRequestInt(request.getParameter(
"width"
),
0
);??
-
????????
int
?height?=?commons.getRequestInt(request.getParameter(
"height"
),
0
);??
-
??????????
-
????????BufferedImage?img?=?
new
?BufferedImage(width,?height,??
-
????????????????BufferedImage.TYPE_INT_RGB);??
-
????????????????
try
?{??
-
??????????????
-
????????????
int
?w?=?width;??
-
????????????
int
?h?=?height;??
-
????????????
int
[]?pixels?=?
new
?
int
[w?*?h];??
-
????????????String[]?m_tempPics?=?bitmap_data.split(
","
);??
-
????????????
for
?(
int
?x?=?
0
;?x?<?w;?x++)?{??
-
????????????????
for
?(
int
?y?=?
0
;?y?<?h;?y++)?{??
-
????????????????????????????????????Long?pic_argb?=?Long.parseLong(m_tempPics[x?*?h?+?y]);??
-
????????????????????????????????????????
int
?a?=?(
int
)?(pic_argb?>>?
24
?&?
0xFF
);??
-
????????????????????
int
?r?=?(
int
)?(pic_argb?>>?
16
?&?
0xFF
);??
??
-
????????????????????
int
?g?=?(
int
)?(pic_argb?>>?
8
?&?
0xFF
);??
-
????????????????????
int
?b?=?(
int
)?(pic_argb?&?
0xFF
);??
-
????????????????????????????????????????pixels[y?*?w?+?x]?=?
new
?Color(r,?g,?b,?a).getRGB();??
-
??????????????????????
-
????????????????}??
-
????????????}??
-
????????????img.setRGB(
0
,?
0
,?w,?h,?pixels,?
0
,?w);??
-
????????????img.flush();??
-
????????????ByteArrayOutputStream?bao?=?
new
?ByteArrayOutputStream();??
-
??????????????
-
????????????ImageIO.write(img,?
"jpg"
,?bao);??
-
????????????
byte
[]?data?=?bao.toByteArray();??
-
????????????Personimage?user?=?
new
?Personimage();??
-
????????????user.setFileContent(data);??
-
????????????PersonimageDAO?hm?=?
new
?PersonimageDAO();??
-
????????????hm.save(user);??
-
????????????PersonrelationDAO?prm?=?
new
?PersonrelationDAO();??
-
????????????Personrelation?pr?=?prm.findById(
new
?Long(personRelationId));??
-
????????????pr.setPersonimageid(user.getId());??
-
????????????pr.setImageflag(
"1"
);??
-
????????????prm.update(pr);??
-
????????????str.append(
"保存成功"
);??
-
????????}?
catch
?(Exception?e)?{??
-
????????????e.printStackTrace();??
-
????????????str.append(
"保存失敗"
);??
-
????????}??
-
????????response.setContentType(
"text/xml"
);??
-
????????response.getWriter().write(str.toString());??
-
????}??
-
??