首页 > 解决方案 > 如何将图像高质量快速上传到服务器

问题描述

实际上我正在尝试将图像上传到具有出色质量的服务器。目前我正在将位图数组转换为base64字符串arrayList,然后将其发送到服务器socket.io。但问题是当我试图提高位图base64转换的质量时花费了很多时间,而且效率很低。即使花了这么多时间,我在服务器中得到的图像质量也很差。所以有人可以帮我解决这个问题并提高上传效率吗?

代码:

public ArrayList<String> convert(ArrayList<Bitmap> bitmap){
    ArrayList<String> pics=new ArrayList<>();
    for(int i=0;i<bitmap.size();i++){
        pics.add(getStringImage(bitmap.get(i)));
    }
    return pics;
}
public static String getStringImage(Bitmap bmp){
    //I've givent maxSize as 500
    int outWidth;
    int outHeight;
    int inWidth = bmp.getWidth();
    int inHeight = bmp.getHeight();
    if(inWidth > inHeight){
        outWidth = maxSize;
        outHeight = (inHeight * maxSize) / inWidth;
    } else {
        outHeight = maxSize;
        outWidth = (inWidth * maxSize) / inHeight;
    }

    Bitmap new_bitmap = Bitmap.createScaledBitmap(bmp, outWidth, outHeight, false);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    new_bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
    byte[] imageBytes = baos.toByteArray();
    String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
    return encodedImage;
}

// Sending to server
final JSONObject obji=new JSONObject();
                    try {
                        //bit is arraylist of bitmap
                        obji.put("uploads_username",username);
                        obji.put("uploads",convert(bit));
                        socket.emit("data",obji);
                        }catch(Exception e){
                                }

标签: androidimagememory-managementandroid-bitmap

解决方案


推荐阅读