gpt4 book ai didi

android 将图像转换为 base64 for php

转载 作者:行者123 更新时间:2023-11-30 01:40:36 24 4
gpt4 key购买 nike

Base64格式图片的制作方法 写一个php服务器用一套标准的图片大小set size

Bitmap bm = Bitmap.createScaledBitmap(MediaStore.Images.Media.getBitmap(getContentResolver(), filePath), sizeW, sizeH, true)

最佳答案

    int sizeW=MediaStore.Images.Media.getBitmap(getContentResolver(), filePath).getWidth();
int sizeH=MediaStore.Images.Media.getBitmap(getContentResolver(), filePath).getHeight();
Matrix m = new Matrix();
m.setRectToRect(new RectF(0, 0,sizeW, sizeH), new RectF(0, 0, 300, 100), Matrix.ScaleToFit.FILL);
Bitmap bm = Bitmap.createBitmap(MediaStore.Images.Media.getBitmap(getContentResolver(), filePath), 0, 0, sizeW, sizeH, m, true);

//Bitmap bm = Bitmap.createScaledBitmap(MediaStore.Images.Media.getBitmap(getContentResolver(), filePath), sizeW, sizeH, true);

ByteArrayOutputStream bao = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 90, bao);
byte[] ba = bao.toByteArray();
//Log.d("size200",String.valueOf((bm.getByteCount()/1024)/2));
ba1 = Base64.encodeToString(ba, Base64.DEFAULT);
Log.e("base64-1", "-----" + ba1);

AsyncTask 类:

   public class uploadToServer extends AsyncTask<Void, Void, String> {

private ProgressDialog pd = new ProgressDialog(RegisterAds.this);
protected void onPreExecute() {
super.onPreExecute();
pd.setMessage("send");
pd.show();
}

@Override
protected String doInBackground(Void... params) {

ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("base64", ba1));
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(URL);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
String st = EntityUtils.toString(response.getEntity());
Log.v("log_tag", "In the try Loop" + st);

} catch (Exception e) {
Log.v("log_tag", "Error in http connection " + e.toString());
}
return "Success";

}

protected void onPostExecute(String result) {
super.onPostExecute(result);
pd.hide();
Log.d("nameimage", String.valueOf(name_image_upload_i1));
new SummaryAsyncTask().execute((Void) null);
pd.dismiss();
}
}

使用:

 new uploadToServer().execute();

php代码:

    <?php
//error_reporting(E_ALL);
if(isset($_POST['ImageName'])){
$imgname = $_POST['ImageName'];
$imsrc = base64_decode($_POST['base64']);
$save_path=$_SERVER['DOCUMENT_ROOT']."/news/upload/".$imgname; //.$data;
header("Content-Type: bitmap; charset=utf-8");
$fp = fopen($save_path, 'w');
fwrite($fp, $imsrc);
if(fclose($fp)){
echo "Image uploaded";
}else{
echo "Error uploading image";}}
?>

关于android 将图像转换为 base64 for php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34562544/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com