图像转换封装工具类BitmapUtil:


/*** 图片位图转换工具** @author lichong* 2022年07月26日15:35:16*/
public class BitmapUtil {public static final String TAG = BitmapUtil.class.getSimpleName();// 把位图数据保存到指定路径的图片文件public static void saveImage(String path, Bitmap bitmap) {// 根据指定的文件路径构建文件输出流对象try (FileOutputStream fos = new FileOutputStream(path)) {// 把位图数据压缩到文件输出流中bitmap.compress(Bitmap.CompressFormat.JPEG, 80, fos);} catch (Exception e) {e.printStackTrace();}}// 图片黑白效果public static Bitmap convertBlack(Bitmap origin) {int width = origin.getWidth(); // 获取位图的宽int height = origin.getHeight(); // 获取位图的高Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);int[] pixels = new int[width * height]; // 通过位图的大小创建像素点数组origin.getPixels(pixels, 0, width, 0, 0, width, height);int alpha = 0xFF << 24;for (int i = 0; i < height; i++) {for (int j = 0; j < width; j++) {int grey = pixels[width * i + j];int red = ((grey & 0x00FF0000) >> 16);int green = ((grey & 0x0000FF00) >> 8);int blue = (grey & 0x000000FF);grey = (int) (red * 0.3 + green * 0.59 + blue * 0.11);grey = alpha | (grey << 16) | (grey << 8) | grey;pixels[width * i + j] = grey;}}bitmap.setPixels(pixels, 0, width, 0, 0, width, height);return bitmap;}// 图片怀旧效果public static Bitmap convertOld(Bitmap origin) {int width = origin.getWidth();int height = origin.getHeight();Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);int pixColor = 0;int pixR = 0, pixG = 0, pixB = 0, newR = 0, newG = 0, newB = 0;int[] pixels = new int[width * height];origin.getPixels(pixels, 0, width, 0, 0, width, height);for (int i = 0; i < height; i++) {for (int k = 0; k < width; k++) {pixColor = pixels[width * i + k];pixR = Color.red(pixColor);pixG = Color.green(pixColor);pixB = Color.blue(pixColor);newR = (int) (0.393 * pixR + 0.769 * pixG + 0.189 * pixB);newG = (int) (0.349 * pixR + 0.686 * pixG + 0.168 * pixB);newB = (int) (0.272 * pixR + 0.534 * pixG + 0.131 * pixB);int newColor = Color.argb(255, newR > 255 ? 255 : newR,newG > 255 ? 255 : newG, newB > 255 ? 255 : newB);pixels[width * i + k] = newColor;}}bitmap.setPixels(pixels, 0, width, 0, 0, width, height);return bitmap;}// 图片底片效果public static Bitmap convertNegative(Bitmap origin) {int width = origin.getWidth();int height = origin.getHeight();Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);int[] oldPixels = new int[width * height];int[] newPixels = new int[width * height];int color, pixelsR, pixelsG, pixelsB, pixelsA;origin.getPixels(oldPixels, 0, width, 0, 0, width, height);for (int i = 1; i < height * width; i++) {color = oldPixels[i];// 获取RGB分量pixelsA = Color.alpha(color);pixelsR = Color.red(color);pixelsG = Color.green(color);pixelsB = Color.blue(color);// 转换pixelsR = (255 - pixelsR);pixelsG = (255 - pixelsG);pixelsB = (255 - pixelsB);// 均小于等于255大于等于0if (pixelsR > 255) {pixelsR = 255;} else if (pixelsR < 0) {pixelsR = 0;}if (pixelsG > 255) {pixelsG = 255;} else if (pixelsG < 0) {pixelsG = 0;}if (pixelsB > 255) {pixelsB = 255;} else if (pixelsB < 0) {pixelsB = 0;}// 根据新的RGB生成新像素newPixels[i] = Color.argb(pixelsA, pixelsR, pixelsG, pixelsB);}bitmap.setPixels(newPixels, 0, width, 0, 0, width, height);return bitmap;}// 图片模糊效果public static Bitmap convertBlur(Bitmap origin) {int width = origin.getWidth();int height = origin.getHeight();int hRadius = width > 150 ? width / 150 : 1; // 水平方向模糊度int vRadius = height > 150 ? height / 150 : 1; // 垂直方向模糊度int iterations = 7; // 模糊迭代度int[] inPixels = new int[width * height];int[] outPixels = new int[width * height];Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);origin.getPixels(inPixels, 0, width, 0, 0, width, height);for (int i = 0; i < iterations; i++) {blur(inPixels, outPixels, width, height, hRadius);blur(outPixels, inPixels, height, width, vRadius);}blurFractional(inPixels, outPixels, width, height, hRadius);blurFractional(outPixels, inPixels, height, width, vRadius);bitmap.setPixels(inPixels, 0, width, 0, 0, width, height);return bitmap;}private static void blur(int[] in, int[] out, int width, int height, int radius) {int widthMinus1 = width - 1;int tableSize = 2 * radius + 1;int[] divide = new int[256 * tableSize];for (int i = 0; i < 256 * tableSize; i++) {divide[i] = i / tableSize;}int inIndex = 0;for (int y = 0; y < height; y++) {int outIndex = y;int ta = 0, tr = 0, tg = 0, tb = 0;for (int i = -radius; i <= radius; i++) {int rgb = in[inIndex + clamp(i, 0, width - 1)];ta += (rgb >> 24) & 0x99; // 调整灰度。0x99表示半透明tr += (rgb >> 16) & 0xff; // 调整红色tg += (rgb >> 8) & 0xff; // 调整绿色tb += rgb & 0xff; // 调整蓝色}for (int x = 0; x < width; x++) {out[outIndex] = (divide[ta] << 24) | (divide[tr] << 16) | (divide[tg] << 8) | divide[tb];int i1 = x + radius + 1;if (i1 > widthMinus1) {i1 = widthMinus1;}int i2 = x - radius;if (i2 < 0) {i2 = 0;}int rgb1 = in[inIndex + i1];int rgb2 = in[inIndex + i2];ta += ((rgb1 >> 24) & 0xff) - ((rgb2 >> 24) & 0xff);tr += ((rgb1 & 0xff0000) - (rgb2 & 0xff0000)) >> 16;tg += ((rgb1 & 0xff00) - (rgb2 & 0xff00)) >> 8;tb += (rgb1 & 0xff) - (rgb2 & 0xff);outIndex += height;}inIndex += width;}}public static void blurFractional(int[] in, int[] out, int width, int height, float radius) {radius -= (int) radius;float f = 1.0f / (1 + 2 * radius);int inIndex = 0;for (int y = 0; y < height; y++) {int outIndex = y;out[outIndex] = in[0];outIndex += height;for (int x = 1; x < width - 1; x++) {int i = inIndex + x;int rgb1 = in[i - 1];int rgb2 = in[i];int rgb3 = in[i + 1];int a1 = (rgb1 >> 24) & 0xff;int r1 = (rgb1 >> 16) & 0xff;int g1 = (rgb1 >> 8) & 0xff;int b1 = rgb1 & 0xff;int a2 = (rgb2 >> 24) & 0xff;int r2 = (rgb2 >> 16) & 0xff;int g2 = (rgb2 >> 8) & 0xff;int b2 = rgb2 & 0xff;int a3 = (rgb3 >> 24) & 0xff;int r3 = (rgb3 >> 16) & 0xff;int g3 = (rgb3 >> 8) & 0xff;int b3 = rgb3 & 0xff;a1 = a2 + (int) ((a1 + a3) * radius);r1 = r2 + (int) ((r1 + r3) * radius);g1 = g2 + (int) ((g1 + g3) * radius);b1 = b2 + (int) ((b1 + b3) * radius);a1 *= f;r1 *= f;g1 *= f;b1 *= f;out[outIndex] = (a1 << 24) | (r1 << 16) | (g1 << 8) | b1;outIndex += height;}out[outIndex] = in[width - 1];inIndex += width;}}

应用:

package top.lc951.myandroid.activity;import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;import top.lc951.myandroid.R;
import top.lc951.myandroid.utils.BitmapUtil;/*** 图像色彩模式:黑白、模糊、老照片、胶卷等*/
public class BitmapColorModeActivity extends AppCompatActivity {private ImageView pictureIv;private Bitmap mOriginBitmap;public static void actionActivity(Context context) {Intent intent = new Intent(context, BitmapColorModeActivity.class);context.startActivity(intent);}@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_bitmap_color_mode);pictureIv = findViewById(R.id.iv_picture);initSpinner();}private String[] colorNameArray = {"原色", "黑白", "底片", "怀旧", "模糊"};private void initSpinner() {mOriginBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_img06);ArrayAdapter<String> colorAdapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1, colorNameArray);Spinner spinner = findViewById(R.id.spinner);spinner.setAdapter(colorAdapter);spinner.setSelection(0);spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {@Overridepublic void onItemSelected(AdapterView<?> parent, View view, int position, long id) {Bitmap bitmap;switch (position) {case 0:bitmap=mOriginBitmap;break;case 1:bitmap = BitmapUtil.convertBlack(mOriginBitmap); // 转换为黑白效果break;case 2:bitmap = BitmapUtil.convertNegative(mOriginBitmap); // 转换为底片效果break;case 3:bitmap = BitmapUtil.convertOld(mOriginBitmap); // 转换为怀旧效果break;case 4:bitmap = BitmapUtil.convertBlur(mOriginBitmap); // 转换为模糊效果break;default:bitmap=mOriginBitmap;}pictureIv.setImageBitmap(bitmap);}@Overridepublic void onNothingSelected(AdapterView<?> parent) {}});}}

【Android】Bitmap图像色彩模式:黑白、模糊、老照片、胶卷等(92/100)相关推荐

  1. Android Bitmap图像优化

    试一试:点击下载. 在Android应用开发中不可避免的会用到图形图像,这样就会生成Bitmap对象.如果在开发过程中没有处理好Bitmap对象就很容易产生Out Of Memory(OOM)的异常. ...

  2. 图像超清化+老照片修复技术,拯救所有模糊和黑白的照片

    也许你曾从柜子里翻出家人们压箱底的老照片,它们已经变黄.变脆,甚至褪色:科技的进步AI智能图像修复技术--基于领先的深度学习技术,将使这些照片奇迹般地回归. 从古老的胶片相机到今天的数字时代,人类捕捉 ...

  3. 【Android 内存优化】Bitmap 图像尺寸缩小 ( 考虑像素密度、针对从不同像素密度资源中解码对应的 Bitmap 对象 | inDensity | inTargetDensity )

    文章目录 一.像素密度对解码图片的影响 二.不考虑像素密度会导致图片缩小尺寸不准确 三.DisplayMetrics 源码阅读.研究手机资源获取规则 四.像素密度参数设置取值 ( inDensity ...

  4. 【Android 内存优化】Bitmap 图像尺寸缩小 ( 设置 Options 参数 | inJustDecodeBounds | inSampleSize | 工具类实现 )

    文章目录 一.解码图片参数 inJustDecodeBounds 二.计算图片的缩小比例 三.设置图片缩小配置 inSampleSize 四.设置图片像素格式 inPreferredConfig 五. ...

  5. android平台下基于ANativeWindow实现渲染bitmap图像

    OpenGL ES 3.0学习实践 android平台下OpenGL ES 3.0从零开始 android平台下OpenGL ES 3.0绘制纯色背景 android平台下OpenGL ES 3.0绘 ...

  6. CVPR 2020丨图像超清化+老照片修复技术,拯救你所有的模糊、破损照片

    编者按:也许你曾从橱柜里翻出家人们压箱底的老照片,而它们已经泛黄发脆,甚至有些褪色:也许你在拍照时不慎手抖,只好把糊成一片的照片都丢进"最近删除".而微软亚洲研究院在计算机视觉顶会 ...

  7. IOS – OpenGL ES 图像侵蚀边缘黑白模糊 GPUImageErosionFilter

    目录 一.简介 二.效果演示 三.源码下载 四.猜你喜欢 零基础 OpenGL (ES) 学习路线推荐 : OpenGL (ES) 学习目录 >> OpenGL ES 基础 零基础 Ope ...

  8. (4.6.31)Android Bitmap 详解

    文章目录 一.从相册加载一张图片 1.1 打开相册加载图片 1.2 根据Uri得到Bitmap 二.Bitmap 内存计算方式 2.1 density 和 densityDpi 2.2 getByte ...

  9. 【Android】图像像素点理解

    学而不思则罔,思而不学则殆 [Android]图像像素点理解 前言 图像处理 -ARGB初始 原图 Alpha R值测试 G值测试 B值测试 非R值测试 非G值测试 非B值测试 高级图片处理 取反1 ...

最新文章

  1. 90后清华女孩:博二开始研究世界级难题,3年发5篇Science,现入选中国榜“35岁以下科技创新35人”!...
  2. dlopen dlsym dlclose加载动态链接库
  3. 【英语学习】【Level 07】U04 Rest and Relaxation L6 Your home away from home
  4. 【优化调度】基于matlab人工鱼群算法求解梯级水库调度优化问题【含Matlab源码 415期】
  5. CNDS-Markdown之公式编辑(一)
  6. opencv学习——翻转摄像头
  7. 北京云栖AI大热 驻云CloudCare揭云服务新趋势
  8. 魔兽、星际和红警的比较
  9. 记一次奇怪的truecrypt解密,隐藏分区的MasterKey
  10. u3d:200个插件免费分享
  11. 广大银行java 面试_2018光大银行春季招聘面试题目及答案
  12. 【3D目标检测】点云数据 To 360度全景图
  13. 有哪些便宜好用的虚拟主机推荐?
  14. PCB板布线经验~~
  15. gpu超算算法_GPU: 超算加速
  16. JavaScript实现11位手机号码正则表达式
  17. RPA机器人眼中的税务工作
  18. Android实现监听Settings值变化的功能
  19. LES-整车企业物料拉动系统的设计和实现
  20. 微信流量主开通后怎么插入广告

热门文章

  1. 汽车融资租赁公司如何实现业务流程化管理和强大风控管理
  2. 在东南亚通过活动拉新两百万用户的背后思考
  3. Excel的54个重要函数
  4. RFID智能书柜的功能与应用
  5. 华为C8650如何实现USB调试 另:网络调试
  6. python第二周练习 风车
  7. 狗网官网farlucky 官网可直接取回CSGO饰品皮肤网页开箱网站
  8. Outlook的设置
  9. 在html5绘制直线的两个方法,html5 Canvas画图教程(2)—画直线与设置线条的样式如颜色/端点/交汇点...
  10. python数据分析-聚类分析