即时通讯在Android应用程序中使用此代码,从相机捕获图像并发送到javascript文件发送一个php控制器用于创建文件并将一些信息插入到mysql中。
这个过程运行正常,但结果图像很小而质量很差
if( ACTION_TAKE_PICTURE == requestCode )
{
Bundle extras = intent.getExtras();
Bitmap bitmap;
if( null == extras || null == (bitmap=(Bitmap)extras.get("data")) ) return;
ByteArrayOutputStream out = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
byte [] ba = out.toByteArray();
String b64img = Base64.encodeToString(out.toByteArray(), Base64.NO_WRAP);
try { out.close(); } catch(Exception e) {}
wv.loadUrl( String.format("%s%s('%s')", getString(R.string.js_call_prefix), getString(R.string.js_callback_camera), b64img ));
}
php代码是:
public function executeImagen(sfWebRequest $request) {
$id = $request->getParameter('id');
$img = $request->getParameter('img');
$category = $request->getParameter('category');
$user = $this->getUser()->getAttribute('id_users');
try {
$basepdir = $path = sfConfig::get("sf_upload_dir") . '/photos/';
$basename = date('Ymd_His');
$buffer = base64_decode(str_replace(' ', '+', $img));
$pname = sprintf("%s%s.JPG", $basepdir, $basename);
$photo = sprintf("%s.JPG", $basename);
$fd = fopen($pname, "wb");
if ($fd) {
fwrite($fd, $buffer);
fflush($fd);
fclose($fd);
}
} catch (Exception $exc) {
echo $exc->getTraceAsString();
}
try {
$tabla = Doctrine::getTable('StorePhotos');
$tabla = new StorePhotos();
$tabla->id_store = $id;
$tabla->id_category = $category;
$tabla->id_users = $user;
$tabla->imagen = $photo;
$tabla->photo_date = date('Y-m-d H:i:s');
$tabla->save();
} catch (Exception $e) {
trigger_error("ERROR: ", $e->getMessage());
}
return sfView::NONE;
}
感谢。