今天我想和大家讨论几个问题,就如文章标题所言:
一、图片压缩
二、图片上传
先叙述第一个问题吧
现在问题来了,压缩放后,图片的存在方式有哪些?
这里总结了一下:
1.文件形式(即以二进制形式存在于硬盘上)
2.流的形式(即以二进制形式存在于内存中)
3.Bitmap形式
下面是常见的压缩方式:
1、将图片保存到本地时进行压缩, 即将图片从Bitmap形式变为File形式时进行压缩,
特点是: File形式的图片确实被压缩了, 但是当你重新读取压缩后的file为 Bitmap是,它占用的内存并没有改变
public static void compressBmpToFile(Bitmap bmp,File file){ ByteArrayOutputStream baos = new ByteArrayOutputStream(); int options = 90; bmp.compress(Bitmap.CompressFormat.JPEG, options, baos); while (baos.toByteArray().length / 1024 > 100) { baos.reset(); options -= 10; bmp.compress(Bitmap.CompressFormat.JPEG, options, baos); } try { FileOutputStream fos = new FileOutputStream(file); fos.write(baos.toByteArray()); fos.flush(); fos.close(); } catch (Exception e) { e.printStackTrace(); } }
方法说明: 该方法是压缩图片的质量, 注意它不会减少图片的像素,比方说, 你的图片是300K的, 1280*700像素的, 经过该方法压缩后, File形式的图片是在100以下, 以方便上传服务器, 但是你BitmapFactory.decodeFile到内存中,变成Bitmap时,它的像素仍然是1280*700, 计算图片像素的方法是 bitmap.getWidth()和bitmap.getHeight(), 图片是由像素组成的, 每个像素又包含什么呢? 熟悉PS的人知道, 图片是有色相,明度和饱和度构成的.
该方法的官方文档也解释说, 它会让图片重新构造, 但是有可能图像的位深(即色深)和每个像素的透明度会变化,JPEG onlysupports opaque(不透明), 也就是说以jpeg格式压缩后, 原来图片中透明的元素将消失.所以这种格式很可能造成失真
既然它是改变了图片的显示质量, 达到了对File形式的图片进行压缩, 图片的像素没有改变的话, 那重新读取经过压缩的file为Bitmap时, 它占用的内存并不会少.(不相信的可以试试)
因为: bitmap.getByteCount() 是计算它的像素所占用的内存, 请看官方解释: Returns the number of bytes used to store this bitmap's pixels.
2、 将图片从本地读到内存时,进行压缩 ,即图片从File形式变为Bitmap形式
特点: 通过设置采样率, 减少图片的像素, 达到对内存中的Bitmap进行压缩
private Bitmap compressBmpFromBmp(Bitmap image) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); int options = 100; image.compress(Bitmap.CompressFormat.JPEG, 100, baos); while (baos.toByteArray().length / 1024 > 100) { baos.reset(); options -= 10; image.compress(Bitmap.CompressFormat.JPEG, options, baos); } ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray()); Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null); return bitmap; }
方法说明: 该方法是对内存中的Bitmap进行质量上的压缩, 由上面的理论可以得出该方法是无效的, 而且也是没有必要的,因为你已经将它读到内存中了,再压缩多此一举, 尽管在获取系统相册图片时,某些手机会直接返回一个Bitmap,但是这种情况下, 返回的Bitmap都是经过压缩的, 它不可能直接返回一个原声的Bitmap形式的图片, 后果可想而知
图片压缩暂且告一段落。
关于图片上传,本人有个想法却没有具体实现过,下面我用给大家描述下,有什么不妥或者错误的地方请指出 !
假如图片是以bitmap形式存储在硬盘上,现在的需求是从sd卡中选取多张图片。然后上传到服务器上,如何上传呢?
下面这个方法大家看下就会懂了. 而我最初的想法却不是下面代码的思想,但是也差不多吧....
我是自己这样想的,将单张bitmap 转成byte[],在将多个byte[] 合并成一个byte[] ,最后将合并后的byte[] toBase64String,也就是以string流的形式传输,服务器端接收后反顺序解析,仅是个人的想法,有什么错误的理解请予以指出。
private void doFileUpload(){ File file1 = new File(selectedPath1); File file2 = new File(selectedPath2); String urlString = "http://10.0.2.2/upload_test/upload_media_test.php"; try { HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost(urlString); FileBody bin1 = new FileBody(file1); FileBody bin2 = new FileBody(file2); MultipartEntity reqEntity = new MultipartEntity(); reqEntity.addPart("uploadedfile1", bin1); reqEntity.addPart("uploadedfile2", bin2); reqEntity.addPart("user", new StringBody("User")); post.setEntity(reqEntity); HttpResponse response = client.execute(post); resEntity = response.getEntity(); final String response_str = EntityUtils.toString(resEntity); if (resEntity != null) { Log.i("RESPONSE",response_str); runOnUiThread(new Runnable(){ public void run() { try { res.setTextColor(Color.GREEN); res.setText("n Response from server : n " + response_str); Toast.makeText(getApplicationContext(),"Upload Complete. Check the server uploads directory.", Toast.LENGTH_LONG).show(); } catch (Exception e) { e.printStackTrace(); } } }); } } catch (Exception ex){ Log.e("Debug", "error: " + ex.getMessage(), ex); } }
ps:文章的内容并不是自己的原创,只是小弟整理后加上自己的一些见解,如有损害您的利益 ,请联系本人,核实后予以删除
作者:__且末流年