图片(Image)压缩
来源:互联网 发布:clipstudiopaint mac 编辑:程序博客网 时间:2024/05/01 23:25
网络访问权限
<uses-permission android:name="android.permission.INTERNET"/>
MainActivity
public class MainActivity extends AppCompatActivity { private ImageView ivIcon; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ivIcon = (ImageView) findViewById(R.id.ivIcon); } public void btnLoadImge(View v){ loadImage("http://20.1.4.181:8080/beautiful.jpg",100,100); } /** * 加载图片,并压缩 * @param imgPath * @param requestWidth * @param requestHeight */ public void loadImage(String imgPath, final int requestWidth, final int requestHeight){ new AsyncTask<String,Void,Bitmap>(){ @Override protected void onPostExecute(Bitmap bitmap) { super.onPostExecute(bitmap); if(bitmap != null){ ivIcon.setImageBitmap(bitmap); } } @Override protected Bitmap doInBackground(String... params) { try { //得到图片路径 String path = params[0]; URL url = new URL(path); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setConnectTimeout(5000); connection.setReadTimeout(5000); int code = connection.getResponseCode(); if(code == 200){ //得到图片流 InputStream is = connection.getInputStream(); //解析图片 BitmapFactory.Options options = new BitmapFactory.Options(); //让图片暂时不去解析 只需要获取图片的宽高 options.inJustDecodeBounds = true; //解析一下图片,这个时候,只会解析图片的宽*高 BitmapFactory.decodeStream(is,null,options); //计算采样率 options.inSampleSize = ImageResizer.calculateInSampleSize(options,requestWidth,requestHeight); //我这时候需要真正去解析图片 options.inJustDecodeBounds = false; //关闭流 is.close(); //得到图片的输入流 is = url.openStream(); //解析图片的时候,才会去真正解析 Bitmap bitmap = BitmapFactory.decodeStream(is,null,options); //关闭流 is.close(); return bitmap; } } catch (Exception e) { e.printStackTrace(); } return null; } }.execute(imgPath); }}
计算采样率
public class ImageResizer { /** * 计算采样率 * @param options * @param requestWidth * @param requestHeight * @return */ public static int calculateInSampleSize(BitmapFactory.Options options,int requestWidth,int requestHeight){ //获取图片的真实的宽高 int width = options.outWidth; int height = options.outHeight; int inSampleSize = 1; options.inSampleSize = inSampleSize;//默认 1 1代表不对图片进行任何压缩 //计算采样率 if(width > requestWidth || height > requestHeight){ int halfWidth = width / 2; int halfHeight = height / 2; while((halfWidth / inSampleSize) >= requestWidth && (halfHeight / inSampleSize) >= requestHeight){ //计算采样率 inSampleSize *= 2; } } return inSampleSize; }}
xml布局
<Button android:onClick="btnLoadImge" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="加载图片(压缩)"/> <ImageView android:id="@+id/ivIcon" android:layout_width="wrap_content" android:layout_height="wrap_content" />
0 0
- Image图片压缩
- 图片(Image)压缩
- java压缩处理Image图片
- Image图片的压缩使用
- Image经典图片压缩方法
- 【图片无损压缩利器】Image Optimizer
- 使用 Image.SCALE_SMOOTH算法进行图片压缩
- 使用 Image.SCALE_SMOOTH算法进行图片压缩
- 分享一个上传图片,图片压缩Unsupported Image Type解决方案
- 分享一个上传图片,图片压缩Unsupported Image Type解决方案
- 分享一个上传图片,图片压缩Unsupported Image Type解决方案
- php image函数,操作压缩图片时,png图片压缩后整个图片变黑
- web前端之HTML5压缩图片compress image with canvas
- 图片压缩时 某个 jpeg图片 报错 Unsupported Image Type
- image图片
- Upload上传+Image压缩
- 把图片压缩并存放到数据库的实现(how to store image into database)
- megapix-image插件 使用Canvas压缩图片上传 MegaPixImage.js下载
- HDU1062 Text Reverse
- ios中域名转换为ip地址
- 去哪儿网机票搜索系统的高并发架构设计
- (二)TCP/IP基础知识--图解TCP/IP读书笔记
- bzoj 4071: [Apio2015]巴邻旁之桥 线段树
- 图片(Image)压缩
- Spring 使用注解方式进行事务管理
- SSM配置
- 亿级Web系统搭建——单机到分布式集群
- Java使用POI为Excel打水印,调整列宽并设置Excel只读
- Mac对摘要进行加密后,怎么对摘要进行解密?
- Android进阶之路
- Cookie与Session的对比
- Qualcomm Android camera 架构简析及如何debug