将位图转换为base64时获取半图像

问题描述 投票:0回答:3

我想将图像上传到服务器,该服务器接受base64编码格式的图像。我使用此代码对位图进行编码,但它只对图像的一半进行编码,而服务器只接收图像部分的一半。

这是我的代码。

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            photo.compress(Bitmap.CompressFormat.PNG, 10, byteArrayOutputStream);
            byte[] byteArray = byteArrayOutputStream .toByteArray();

            String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);
// Send to server(encoded)

当我尝试从图库编码图像时,会弹出错误并导致应用程序崩溃,并显示错误。

java.lang.OutOfMemoryError:无法分配带有13777320个空闲字节的15521174字节分配和13MB直到OOM

我想编码完整的图像,任何帮助?

android image bitmap base64 encode
3个回答
2
投票

试试这段代码:

 BitmapFactory.Options options;
 options = new BitmapFactory.Options();

    // downsizing image as it throws OutOfMemory Exception for larger
    // images
    options.inSampleSize = 6;
   Bitmap bitmap1 = BitmapFactory.decodeFile(fileUri1.getPath(),
                        options);
                ByteArrayOutputStream baos1 = new ByteArrayOutputStream();
                if(bitmap1 != null) {
                    bitmap1.compress(Bitmap.CompressFormat.PNG, 10, baos1);
                    byte[] b1 = baos1.toByteArray();
                    bitmapstring1 = Base64.encodeToString(b1,    

                   Base64.DEFAULT);

1
投票

您必须得到'OutOfMemoryError',因为您可能正在使用分辨率非常高的图像并将其直接加载到内存中,因此base64的字符串太大。始终在内存中加载图像的子采样版本以避免OutOfMemoryErrorTake a look at this.来自文档:

例如,如果最终将在ImageView中以128x96像素的缩略图显示,则不值得将1024x768像素图像加载到内存中。

首先计算图像的样本大小:

public static int calculateInSampleSize(
        BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;

if (height > reqHeight || width > reqWidth) {

    final int halfHeight = height / 2;
    final int halfWidth = width / 2;

    // Calculate the largest inSampleSize value that is a power of 2 and keeps both
    // height and width larger than the requested height and width.
    while ((halfHeight / inSampleSize) >= reqHeight
            && (halfWidth / inSampleSize) >= reqWidth) {
        inSampleSize *= 2;
    }
}

return inSampleSize;
}

然后解码采样版本:

public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
    int reqWidth, int reqHeight) {

// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);

// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}

加载位图的采样版本后,将位图转换为base64。


0
投票
public String getStringImage(Bitmap bmp) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    byte[] imageBytes = baos.toByteArray();
    String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
    Log.d(TAG, "getStringImage: "+encodedImage.trim());
    return encodedImage;
}

试试这个

© www.soinside.com 2019 - 2024. All rights reserved.