Android Bitmap 图片处理工具类详解手机开发
2023-06-13 09:20:07 时间
public static Bitmap drawableToBitmap(Drawable drawable) { Bitmap bitmap = Bitmap .createBitmap( drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565); Canvas canvas = new Canvas(bitmap); // canvas.setBitmap(bitmap); drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); drawable.draw(canvas); return bitmap; }
将Bitmap转为btye[]
public static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) { ByteArrayOutputStream output = new ByteArrayOutputStream(); bmp.compress(CompressFormat.JPEG, 80, output); if (needRecycle) { bmp.recycle(); byte[] result = output.toByteArray(); try { output.close(); } catch (Exception e) { e.printStackTrace(); return result;
将图片存储到sdcard中
public static void storeImageToSDCARD(Bitmap colorImage, String ImageName, String path) { File file = new File(path); if (!file.exists()) { file.mkdir(); File imagefile = new File(file, ImageName + ".jpg"); try { imagefile.createNewFile(); FileOutputStream fos = new FileOutputStream(imagefile); colorImage.compress(Bitmap.CompressFormat.JPEG, 100, fos); fos.flush(); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
sdcard取图片
public static Bitmap getImg(String path,String picName) { if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { return null; try { File file = new File(path, picName + ".png"); FileInputStream inputStream = new FileInputStream(file); byte[] b = new byte[inputStream.available()]; inputStream.read(b); Bitmap bitmap = BitmapFactory.decodeByteArray(b, 0, b.length); return bitmap; } catch (Exception e) { return null; }网络获取图片
public static Bitmap getImageByNet(String urlpath) throws Exception { URL url = new URL(urlpath); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5 * 1000); Bitmap bitmap = null; if (conn.getResponseCode() == 200) { InputStream inputStream = conn.getInputStream(); bitmap = BitmapFactory.decodeStream(inputStream); return bitmap; }
将两个bitmap对象整合并保存为一张图片
public Bitmap combineBitmap(Bitmap background, Bitmap foreground) { //第一张图片的宽高 int bgWidth = background.getWidth(); int bgHeight = background.getHeight(); //第二章图片的宽高 int fgWidth = foreground.getWidth(); int fgHeight = foreground.getHeight(); //创建一个新的bitmao 高度等于两张高度的总和 用来竖列拼接 Bitmap newmap = Bitmap.createBitmap(bgWidth, bgHeight + fgHeight, android.graphics.Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(newmap); //画上第一张图片 canvas.drawBitmap(background, 0, 0, null); //从第一张图片的下边开始画入第二张图片 canvas.drawBitmap(foreground, 0, bgHeight, null); return newmap; }Bitmap旋转一定角度
public static Bitmap rotate(Bitmap b, int degrees) { if (degrees != 0 b != null) { Matrix m = new Matrix(); m.setRotate(degrees, (float) b.getWidth() / 2, (float) b.getHeight() / 2); try { Bitmap b2 = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), m, true); return b2;// 正常情况下返回旋转角度的图 } catch (OutOfMemoryError ex) { return b;// 内存溢出返回原图 } finally { b.recycle();// 释放资源 return b; }
Bitmap画一个圆角图
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) { if (bitmap == null) { return bitmap; try { Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888); Canvas canvas = new Canvas(output); final int color = 0xff424242; final Paint paint = new Paint(); final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); final RectF rectF = new RectF(rect); paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); canvas.drawRoundRect(rectF, roundPx, roundPx, paint); paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, rect, paint); return output; } catch (OutOfMemoryError e) { // TODO: handle exception System.gc(); return null; }从view 得到图片
public static Bitmap getBitmapFromView(View view) { view.destroyDrawingCache(); view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec .makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)); view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight()); view.setDrawingCacheEnabled(true); Bitmap bitmap = view.getDrawingCache(true); return bitmap; }添加水印
public static Bitmap watermarkBitmap(Bitmap src, Bitmap watermark, String title) { if (src == null) { return null; int w = src.getWidth(); int h = src.getHeight(); // 需要处理图片太大造成的内存超过的问题,这里我的图片很小所以不写相应代码了 Bitmap newb = Bitmap.createBitmap(w, h, Config.ARGB_8888);// 创建一个新的和SRC长度宽度一样的位图 Canvas cv = new Canvas(newb); cv.drawBitmap(src, 0, 0, null);// 在 0,0坐标开始画入src Paint paint = new Paint(); // 加入图片 if (watermark != null) { int ww = watermark.getWidth(); int wh = watermark.getHeight(); paint.setAlpha(50); cv.drawBitmap(watermark, w - ww + 5, h - wh + 5, paint);// 在src的右下角画入水印 // 加入文字 if (title != null) { String familyName = "宋体"; Typeface font = Typeface.create(familyName, Typeface.BOLD); TextPaint textPaint = new TextPaint(); textPaint.setColor(Color.RED); textPaint.setTypeface(font); textPaint.setTextSize(22); // 这里是自动换行的 StaticLayout layout = new StaticLayout(title, textPaint, w, Alignment.ALIGN_NORMAL, 1.0F, 0.0F, true); layout.draw(cv); // 文字就加左上角算了 // cv.drawText(title,0,40,paint); cv.save(Canvas.ALL_SAVE_FLAG);// 保存 cv.restore();// 存储 return newb; }等比例压缩图片
public static Bitmap getBitmap(Bitmap bitmap, int screenWidth, int screenHight) { int w = bitmap.getWidth(); int h = bitmap.getHeight(); Matrix matrix = new Matrix(); float scale = (float) screenWidth / w; float scale2 = (float) screenHight / h; //取比例小的值 可以把图片完全缩放在屏幕内 scale = scale scale2 ? scale : scale2; // 都按照宽度scale 保证图片不变形.根据宽度来确定高度 matrix.postScale(scale, scale); // w,h是原图的属性. return Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true); }
以最省内存的方式读取本地资源的图片
public static Bitmap ReadBitmapById(Context context, int drawableId, int screenWidth, int screenHight) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inPreferredConfig = Config.ARGB_8888; options.inInputShareable = true; options.inPurgeable = true; InputStream stream = context.getResources().openRawResource(drawableId); Bitmap bitmap = BitmapFactory.decodeStream(stream, null, options); return getBitmap(bitmap, screenWidth, screenHight); }
原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/3239.html
服务器部署程序员系统优化网站设置运维相关文章
- xps 转 pdf android版,OakDoc XPS to PDF Converter(XPS文件转PDF格式工具)V2.2 正式版
- android进阶之了解Android系统与开机过程
- android退出app代码,Android应用退出代码各种方式
- Android FFmpeg系列05--音频解码与播放
- android toast 自定义时间,Android 自定义 Toast 显示时间「建议收藏」
- Android n_android 反编译
- Android Services Library_android freeware
- Android SIM卡 插拔监听
- 【Android 进程保活】提升进程优先级 ( 使用前台 Service 提高应用进程优先级 | 启动相同 id 的第二个前台 Service 关闭通知 )
- 【Android CPU 优化】Android CPU 调优 ( Trace 文件分析 | Android Profiler 工具 | CPU Profiler 工具 )
- 【Android 组件化】路由组件 ( 组件间共享的服务 )
- 【Android 逆向】Android 权限 ( Android 逆向中使用的 android.permission 权限 | Android 系统中的 Linux 用户权限 )
- 【Android 逆向】使用 DB Browser 查看并修改 SQLite 数据库 ( 从 Android 应用数据目录中拷贝数据库文件 | 使用 DB Browser 工具查看数据块文件 )
- 【Android 逆向】Android 进程代码注入原理 ( 进程注入原理 | 远程调用流程 | 获取函数地址 | 设置 IP 寄存器 | mmap 申请内存 | 设置 SP 寄存器 )
- 【Android 逆向】Android 进程注入工具开发 ( 远程调用 | x86 架构的返回值获取 | arm 架构远程调用 )
- 【Android Gradle 插件】Gradle 构建工具简介 ① ( Gradle 环境配置 | 官网下载 Gradle 软件包 | 在本地用户目录下查找 | 配置 Gradle 环境变量 )
- 安装包立减1M--微信Android资源混淆打包工具
- 【错误记录】Android Studio 编译报错 ( Could not resolve com.android.tools.build:gradle:7.4.2. )
- Android选择本地图片并裁剪工具类详解手机开发
- Android开发中遇到的问题(二)——新建android工程的时候eclipse没有生成MainActivity和layout布局详解手机开发
- [android] 看博客学习Android常见的几种RuntimeException详解手机开发
- android Universal Image Loader for Android 说明文档 (1)详解手机开发
- JDBC从搭建服务器,到运行到Android全集锦!!详解编程语言
- Android 与 Linux 的结合:解锁更多精彩(androidlinux)
- Android QQ HD v5.5.4 正式版发布
- Android获取手机配置信息具体实现代码
- android实现简单的乘法计算代码
- android教程之service使用方法示例详解
- Android开发之时间日期操作实例