首页 > 解决方案 > BitmapToBase64 Android Studio

问题描述

我将一个从预览图片转换到服务器的 base64 字符串发布。为了使这个字符串更短,我尝试减小位图的大小。但在那之后,我发现字符串的长度并没有减少。以下是代码。

 @Override
    protected Object doInBackground(Object[] objects) {

        Camera.Parameters parameters = mCamera.getParameters();
        int imageFormat = parameters.getPreviewFormat();
        int w = parameters.getPreviewSize().width;
        int h = parameters.getPreviewSize().height;

        // TODO: Arguments?
        Rect rect = new Rect(0, 0, w, h);
        YuvImage yuvImage = new YuvImage(mData, imageFormat, w, h, null);
        Log.e("Image", "width:" + w + " height:" + h);

        try {
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            yuvImage.compressToJpeg(rect, 100, outputStream);
            Bitmap raw_bitmap = BitmapFactory.decodeByteArray(outputStream.toByteArray(), 0, outputStream.size());
            Log.i(TAG, "onPreviewFrame: origin_raw_bitmap:" + ToBase64.bitmapToBase64(raw_bitmap));
//            raw_bitmap = BitmapProcess.scaleBitmap(raw_bitmap, 50, 50);
            raw_bitmap = BitmapProcess.compressImage(raw_bitmap);
            Log.i(TAG, "onPreviewFrame: new_raw_bitmap:" + ToBase64.bitmapToBase64(raw_bitmap));

            String base64_global = ToBase64.bitmapToBase64(raw_bitmap);

            login(myurl, base64_global, new okhttp3.Callback() {
                @Override
                public void onFailure(Call call, IOException e) {

                    Log.d(TAG + "Failure", "onFailure:err");
//                            Toast.makeText(MainActivity.this, "Connection refused!", Toast.LENGTH_SHORT).show();

                    e.printStackTrace();
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    assert response.body() != null;
                    String result = response.body().string();

                    Log.d(TAG + "Response", "out result===" + result);
                    Log.d(TAG + "Response", response.code() + "");

                    if (JudgeAns(result)) {
                        Log.e("Response", "Blind");

                        Looper.prepare();
                        Toast toast = Toast.makeText(SurfaceViewCallback.getInstacne().context, "Get the Blind way.", Toast.LENGTH_SHORT);
                        toast.setGravity(Gravity.CENTER, 0, 0);
                        toast.show();
                        Looper.loop();

                    } else {
                        Log.e("Response", "Others");
                        Looper.prepare();
                        Toast toast = Toast.makeText(SurfaceViewCallback.getInstacne().context, "Can not find the Blind way.", Toast.LENGTH_SHORT);
                        toast.setGravity(Gravity.CENTER, 0, 0);
                        toast.show();
                        Looper.prepare();
                    }
                }
            });
        } catch (Exception e) {
            Log.e(TAG, "onPreviewFrame: Fail" + e.getLocalizedMessage());
        }
        return null;
    }
public static Bitmap compressImage(Bitmap image) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        int options = 100;
        while (baos.toByteArray().length / 1024 > 100) { 
            baos.reset();
            image.compress(Bitmap.CompressFormat.JPEG, options, baos);
            options -= 10;
        }
        ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
        return BitmapFactory.decodeStream(isBm, null, null);
    }

这些是 Android Studio 中 Logcat 中的信息。

位图缩小前的字符串: 在此处输入图像描述

位图缩小后的字符串: 在此处输入图像描述

标签: javaandroidcamera

解决方案


推荐阅读