在将Base64字符串解码为图像时,我得到一个完全黑色的图像

时间:2017-07-19 20:06:56

标签: android encoding decoding

我已经使用此功能对图像进行编码。

private String encodeImage(Bitmap bm)
        {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            bm.compress(Bitmap.CompressFormat.JPEG,100,baos);
            byte[] b = baos.toByteArray();
            String encImage = Base64.encodeToString(b, Base64.DEFAULT);
            Log.d(TAG, "encodeImage: " + encImage);
            return encImage;
        }

这是我创建位图图像的方式

setContentView(R.layout.camera);
        cameraView = (CameraView) findViewById(cameras);
        cameraView.setJpegQuality(2);
        cameraView.setCameraListener(new CameraListener() {
            @Override
            public void onPictureTaken(byte[] jpeg) {
                super.onPictureTaken(jpeg);

                Bitmap bitmap = BitmapFactory.decodeByteArray(jpeg, 0, jpeg.length);
                encodeImage(bitmap);
            }
        });

enter image description here

在使用在线工具解码由此生成的byte64字符串时,我得到一个完全黑色的图像。

这是我的byte64字符串

/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEB
AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/2wBDAQEBAQEBAQEBAQEBAQEBAQEBAQEB
AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/wAARCBIACiADASIA
AhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQA
AAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3
ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWm
p6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEA
AwEBAQEBAQEBAQAAAAAA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HXtyQCasdxFFMfLk7D36E/wCJOM55xkdaScZlLyH96f8Alv8A
QsfX6/j3xursw8pNy0Vr07+nNPz/ALq+97kqb626dHteV+vZRfzfVMmkSRmMsUsp87Hri5wz8j9T
jryepAYyRIY4448enn9P9GvOQe/r15I5GcAZqJXiDGKPnHE/v8xx3PqR69cZwxq+H8sRyeb/AMsB
5A/4E+e59vfr68lWUot6K15W9FKXn5/l2dzneu

2 个答案:

答案 0 :(得分:2)

您的编码代码没问题,只需检查一下。尝试从logcat复制编码字符串时,很有可能出错。只需在Log.d()上放置断点,复制编码的字符串值并通过在线工具再次检出。我使用this来测试结果。

答案 1 :(得分:0)

我不在Android中编码,但我认为您正在混合数据。例如,manual表示Bitmap bitmap = BitmapFactory.decodeByteArray(jpeg, 0, jpeg.length); “从指定的字节数组中解码不可变位图。”所以这一行:

jpeg

仅当encImage数组包含原始RGB颜色值而非实际JPEG值时才起作用(因为JPEG是压缩数学而不是颜色数组)。

试试这个,看看某些online tool是否可以显示字符串 private String encodeImage(Bitmap bm)。没有使用其他函数setContentView(R.layout.camera); cameraView = (CameraView) findViewById(cameras); cameraView.setPictureFormat(ImageFormat.JPEG); cameraView.setJpegQuality(2); cameraView.setCameraListener(new CameraListener() { @Override public void onPictureTaken(byte[] jpeg) { super.onPictureTaken(jpeg); String encImage = Base64.encodeToString(jpeg, Base64.DEFAULT); //print and test this string in some B64-to-JPEG decoder/viewer //Bitmap bitmap = BitmapFactory.decodeByteArray(jpeg, 0, jpeg.length); //encodeImage(bitmap); } }); ,因为我怀疑它是问题的一部分......

Vue.material.setCurrentTheme('orange');