我有一個flex應用程序,我必須使用base64轉換器轉換相機拍攝的jpeg圖片,然後將結果上載到sqlite數據庫(如MEDIUMBLOB)。代碼如下所示:使用jpeg圖像時Flex Flex應用程序暫停
圖像的MXML聲明:
<s:Group width="480" height="304">
<s:Label x="0" y="0" width="100%" height="34" backgroundColor="#4D4D4D"
color="#FFFFFF" fontSize="30" text=" Select photo" verticalAlign="middle"/>
<s:Image id="image" x="10" y="42" width="217" height="246" />
<s:Image id="image2" x="10" y="42" width="217" height="246" source = "@Embed(source='skins/PhotoNotAvailable.jpg')"/>
<s:Button x="235" y="42" width="235" height="84" label="Take a Picture"
click="captureImage(event)" enabled="{CameraUI.isSupported}" fontSize="30"/>
<s:Button x="235" y="150" width="235" height="70" label="Delete"
click="deletePhoto(event)" fontSize="30"/>
</s:Group>
腳本用於拍照和皈依:現在
//Taking the pictures
protected function application1_applicationCompleteHandler(event:FlexEvent):void {
image.setVisible(true);
image2.setVisible(false);
if (CameraUI.isSupported){
var mediaPromise:MediaPromise;
camera = new CameraUI();
camera.addEventListener(MediaEvent.COMPLETE, onComplete);
camera.addEventListener(ErrorEvent.ERROR, onError);
} else {
}
}
protected function view1_activateHandler(event:Event):void
{
conn = data as SQLConnection;
}
private function captureImage(event:MouseEvent):void {
camera.launch(MediaType.IMAGE);
}
private function onError(event:ErrorEvent):void {
trace("error has occurred");
}
private function onComplete(event:MediaEvent):void {
var mediaPromise:MediaPromise = event.data;
image.source = mediaPromise.file.url;
pictureTaken = true;
}
//Convertion and uploading to database
protected function AddHandler(event:MouseEvent):void
//irrelevant code skipped
insertStmt = new SQLStatement();
insertStmt.sqlConnection = conn;
var insertSQL:String = ("INSERT INTO RecipeDB (RecipeID, Name, Category, Origin, Recipe, Favorite, Image)" + "VALUES (:RecipeID, :Name, :Category, :Origin, :Recipe, :Favorite, :Image)");
insertStmt.text = insertSQL;
if(pictureTaken)
{
var jencoder:JPEGEncoder = new JPEGEncoder(75);
var imageByteArray:ByteArray = jencoder.encode(image.bitmapData);
var baseEncoder:Base64Encoder = new Base64Encoder();
baseEncoder.encodeBytes(imageByteArray);
encodedBytes = baseEncoder.toString();
}
else
{
var jencoder:JPEGEncoder = new JPEGEncoder(75);
var imageByteArray:ByteArray = jencoder.encode(image2.bitmapData);
var baseEncoder:Base64Encoder = new Base64Encoder();
baseEncoder.encodeBytes(imageByteArray);
encodedBytes = baseEncoder.toString();
}
insertStmt.parameters[":RecipeID"] = ID as int;
insertStmt.parameters[":Name"] = NameArea.text;
insertStmt.parameters[":Category"] = TypeArea.text;
insertStmt.parameters[":Origin"] = OriginArea.text;
insertStmt.parameters[":Recipe"] = RecipeArea.text;
insertStmt.parameters[":Favorite"] = 0 as int;
insertStmt.parameters[":Image"] = encodedBytes;
insertStmt.execute();
}
//Deleting photo
protected function deletePhoto(event:MouseEvent):void
{
pictureTaken = false;
image.setVisible(false);
image2.setVisible(true);
}
,如果沒有圖片拍攝,程序上傳皮/ PhotoNotAvailable.jpg到數據庫正確,但如果圖片被拍攝或採取和刪除,程序掛起(android詢問是否關閉它或等待)。我檢查了拍攝照片的尺寸,並且沒有超過MEDIUMBLOB的尺寸(照片有大約2 MB)。什麼可能是錯的?
呃,實際上我剛剛完成了這個程序,並且完全禁用了照片功能,因爲它很重要。無論如何,因爲數據庫是從2個來源 - 程序和網站提供的圖片中使用base64,並且由於編碼原因,它不會讓我通過網絡上傳未編碼的BLOB。儘管如此,我還是不知道flex會自動進行編碼,所以感謝這些信息。 – kyooryu 2012-02-26 22:00:13
是的,沒問題。 = d – Chaos7703 2012-05-06 05:22:06