简体   繁体   English

如何解码和编码base64图像,而不会失去质量和尺寸

[英]how to decode and encode base64 image without losing the quality and the size

Am encoding an image from android to base64 with this code: 使用以下代码将图像从android编码到base64:

ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bmp.compress(Bitmap.CompressFormat.PNG, 90, stream); //compress to which format you want.
            byte [] byte_arr = stream.toByteArray();
            image_str = Base64.encodeToString(byte_arr, Base64.DEFAULT);

and than insert it into mysql data base, than decode it with php using that code: 而不是将其插入到mysql数据库中,而不是使用该代码解码它:

$filename_path = md5(time().uniqid()).".jpeg"; 
$decoded=base64_decode($image_str);
 file_put_contents("uploads/".$filename_path,$decoded);
//echo '<img src="uploads/".$filename_path"/>';
echo '<img src="uploads/'.$filename_path.'"/>';

it's working all fine i can see the picture and there is no errors, but the problem is am losing like about 80% of the quality and the size , how can i fix that please. 它工作得很好,我可以看到图片,没有错误,但问题是失去了大约80%的质量和尺寸,我该如何解决这个问题。

I think if you make the compress format JPEG , and reduce the quality of the image to 50 , that should do the trick: 我认为如果你将压缩格式设为JPEG ,并将图像质量降低到50 ,那应该可以解决问题:

ByteArrayOutputStream baos = new  ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 50, baos);
byte [] b = baos.toByteArray();
return Base64.encodeToString(b, Base64.DEFAULT);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM