rpc*_*tts 6 java compression android bytearray bitmap
我正在以下列方式压缩位图
Bitmap bmpSig = getMyBitMap();
int size = bmpSig.getWidth() * bmpSig.getHeight();
ByteArrayOutputStream out = new ByteArrayOutputStream(size);
bmpSig.compress(Bitmap.CompressFormat.JPEG, 100, out);
byte[] bytSig = out.toByteArray();
Run Code Online (Sandbox Code Playgroud)
然后我尝试从字节数组中在Android ImageView中显示图像.当我这样做时,我得到的图像是完全黑色的图像.
ImageView myImg = (ImageView) findViewById(R.id.img_view);
myImg.setImageBitmap(BitmapFactory.decodeByteArray(bytSig, 0, bytSig.length));
Run Code Online (Sandbox Code Playgroud)
我假设是因为我在BitmapFactory.decodeByteArray()之前错过了一个步骤来反转jpeg压缩.或者我误解了压缩是如何工作的?
我没有意识到我的位图(来自Canvas对象)的背景是透明的.由于该位图仅是白色背景上的黑线,因此黑色图像是由于压缩为JPEG而使图像为黑色背景.
我改变了
bmpSig.compress(Bitmap.CompressFormat.JPEG, 100, out);
Run Code Online (Sandbox Code Playgroud)
至
bmpSig.compress(Bitmap.CompressFormat.PNG, 100, out);
Run Code Online (Sandbox Code Playgroud)
它正在按预期工作.