android映像到base64字符串,并发送到PHP

时间:2014-04-21 23:53:25

标签: php android base64

即时通讯在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;
}

感谢。

1 个答案:

答案 0 :(得分:0)

您正在收发并发送缩略图。您需要从磁盘检索完整大小的图像。请阅读Google this guide