Android图片色彩变换实现方法
作者:Marksinoberg 发布时间:2022-03-21 07:23:32
标签:android,图片
最近在做图片相关的应用,所以就各方积累到一些常用的操作,一般来说会有多种方式来实现这一功能,比如
1.采用色度变换
2.采用ColorMatrix颜色矩阵
3.采用对像素点的直接操作
等等,今天就复习一下第一种方式吧,虽然比较单一,得到的结果类型也比较少。
相比较于常见的图片风格变换,一般我们就是换个色彩度,饱和度,亮度等等,这里也恰恰是这个方式
编码思路:
•抽象出图片操作工具类
•创建一个用于操作的Bitmap对象
•使用画布Canvas,画笔Paint
•调色处理,参数控制
•画出Bitmap并返回
•被相关方法调用,得到结果
下面直接上代码吧
首先是布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/imageview"
android:layout_width="match_parent"
android:layout_height="320dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:text="色 度"
android:textSize="18dp"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<SeekBar
android:id="@+id/hueBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="5"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:text="饱和度"
android:textSize="18dp"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<SeekBar
android:id="@+id/saturationBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="5"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:text="亮 度"
android:textSize="18dp"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<SeekBar
android:id="@+id/lumBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="5"
/>
</LinearLayout>
</LinearLayout>
接下来是工具操作类的相关方法
public static Bitmap handleImageLikePS(Bitmap bp,float hue,float saturation,float lum){
Bitmap bitmap=Bitmap.createBitmap(bp.getWidth(), bp.getHeight(),Bitmap.Config.ARGB_8888);
Canvas canvas=new Canvas(bitmap);
Paint paint=new Paint(Paint.ANTI_ALIAS_FLAG);
ColorMatrix hueMatrix=new ColorMatrix();
hueMatrix.setRotate(0, hue);
hueMatrix.setRotate(1, hue);
hueMatrix.setRotate(2, hue);
ColorMatrix saturationMatrix=new ColorMatrix();
saturationMatrix.setSaturation(saturation);
ColorMatrix lumMatrix=new ColorMatrix();
lumMatrix.setScale(lum,lum,lum,1);
ColorMatrix imageMatrix=new ColorMatrix();
imageMatrix.postConcat(hueMatrix);
imageMatrix.postConcat(saturationMatrix);
imageMatrix.postConcat(lumMatrix);
paint.setColorFilter(new ColorMatrixColorFilter(imageMatrix));
canvas.drawBitmap(bp, 0, 0, paint);//此处如果换成bitmap就会仅仅调用一次,图像将不能被编辑
return bitmap;
}
然后是使用类
package com.example.colormatrixdemo;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.SeekBar;
public class MainActivity extends Activity implements SeekBar.OnSeekBarChangeListener{
private Bitmap bitmap;
private ImageView imageview;
private SeekBar hueBar,saturationBar,lumBar;
private float mHue,mSaturation ,mLum;
private static int MAXVALUE=255,MIDVALUE=127;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.masuo);
imageview=(ImageView) findViewById(R.id.imageview);
hueBar=(SeekBar) findViewById(R.id.hueBar);
saturationBar=(SeekBar) findViewById(R.id.saturationBar);
lumBar=(SeekBar) findViewById(R.id.lumBar);
hueBar.setOnSeekBarChangeListener(this);
saturationBar.setOnSeekBarChangeListener(this);
lumBar.setOnSeekBarChangeListener(this);
hueBar.setMax(MAXVALUE);
hueBar.setProgress(MIDVALUE);
saturationBar.setMax(MAXVALUE);
saturationBar.setProgress(MIDVALUE);
lumBar.setMax(MAXVALUE);
lumBar.setProgress(MIDVALUE);
imageview.setImageBitmap(bitmap);
}
@Override
public void onProgressChanged(SeekBar seekbar, int progress, boolean arg2) {
switch(seekbar.getId()){
case R.id.hueBar:
mHue=(progress-MIDVALUE)*1.0F/MIDVALUE*180;
break;
case R.id.saturationBar:
mSaturation=progress*1.0F/MIDVALUE;
break;
case R.id.lumBar:
mLum=progress*1.0F/MIDVALUE;
break;
}
imageview.setImageBitmap(ImageTools.handleImageLikePS(bitmap, mHue, mSaturation, mLum));
}
@Override
public void onStartTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
@Override
public void onStopTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
}
然后运行程序,你就可以通过对滑动条的调节来对图像做相关的处理变换了。
注意:
在工具类的方法中最后要对传进去的参数做处理,而不是我们自己声明的bitmap,否则我们将得不到我们实时的图片效果。因为我们的bitmap仅仅是作为一个操作的对象模型,真正需要操作的是我们的bp参数。
总结:在处理图像有许多的方法,尤其是对图像用像素点的方式效果最多,可以呈现多种多样的效果。如老照片,浮雕,底片等等;而采用颜色矩阵也是一种好经典的操作方法。这些很值得我们学习,这样我们就可以是的我们的应用呈现出更加绚丽的色彩及效果咯!


猜你喜欢
- 一、简介当我们没有在子类构造函数中写上 base(),默认会先调用父类中无参的构造函数,再调用子类。当在有参构造函数后写上base时,只调用
- 本过程是使用Virtual Studio 2019实现的,利用老师给出的框架进行的修改。一、认识NetworkStream(网络流)1、Ne
- 在初始化自己位置的时候请求定位权限:Constants.ACCESS_FINE_LOCATION_COMMANDS_REQUEST_CODE
- 现在视频应用越来越火,Periscope火起来后,国内也出现了不少跟风者,界面几乎跟Periscope一模一样.Periscope确实不错,
- 使用IDEA配置Maven搭建开发框架ssm教程一、配置Maven环境1.下载Maven:下载链接2.下载完成解压压缩包并创建本地仓库文件夹
- 如何只返回实体类中的部分字段在实体类上添加注解@JsonInclude(JsonInclude.Include.NON_EMPTY)表示实体
- 一、FeignClient注解FeignClient注解被@Target(ElementType.TYPE)修饰,表示FeignClient
- RMI 介绍RMI (Remote Method Invocation) 模型是一种分布式对象应用,使用 RMI 技术可以使一个 JVM 中
- 一、this用类名定义一个变量的时候,定义的应该只是一个引用,外面可以通过这个引用来访问这个类里面的属性和方法,那们类里面是够也应该有一个引
- 一、OpenCV OpenCV(开源计算机视觉库)是一个开源的计算机视觉和机器学习软件库,是一个基于C与C++的跨平台计算机视觉处
- 直接上代码吧。昨晚腾讯在线测试遇到的题。螺旋矩阵是指一个呈螺旋状的矩阵,它的数字由第一行开始到右边不断变大,向下变大,向左变大,向上变大,如
- 本文实例为大家分享了Android设置默认锁屏壁纸接口的具体代码,供大家参考,具体内容如下完成自定义service后,接下来就是具体实现接口
- 一 应用规划: ※ 确定功能。 ※ 必须的界面及界面跳转的流程。
- activity_list.xml文件代码如下:<?xml version="1.0" encoding=&quo
- 问题描述:使用Design包的TabLayout实现类似网易选项卡动态滑动效果的时候,使用addTab()方法给TabLayout动态添加标
- 如何将ResultSet结果集遍历到List中今天在使用jstl标签展示查询结果时遇到一个小问题,即如何将ResultSet对象传递给前台页
- 附GitHub源码:WebViewExplore一、WebView的基础配置WebSettings ws = getSettings();w
- cin的返回值今天在用STL时用到while(cin>>s1>>a>>s2>>b)这样的语句
- 本文实例讲述了C#集合遍历时删除和增加元素的方法。分享给大家供大家参考,具体如下:大多数时候,遍历集合元素的时候并不需要对元素进行增加或者删
- 上篇文章给大家介绍了,喜欢的朋友点击查看下。SpringBoot 开发提速神器 Lombok+MybatisPlus+SwaggerUILo