Android 图像处理入门(上),面试字节跳动的Android工程师该怎么准备

本文介绍了如何在Android中实现图像处理,特别是通过矩阵变换调整色相、饱和度和亮度。文章详细展示了如何使用ColorMatrix进行矩阵变换,包括创建工具类ImageUtils处理图像,以及在UI中使用Seekbar来控制图像效果。通过分析矩阵变换的原理,阐述了Android图像处理的基本思想和方法。
摘要由CSDN通过智能技术生成

}

三原色调整界面

可以大致分析一下,如果要调整一张图片,要经过这三个步骤:

  1. 获取一张图片

  2. 对图片进行一系列修改

  3. 返回一张图片

因此,不妨设计一个工具类,来进行图像相关的处理,这样可以提高代码的复用性。

public class ImageUtils {

public static Bitmap handleImageEffect(Bitmap bitmap,float hue,float saturation,float lum){

//由于不能直接在原图上修改,所以创建一个图片,设定宽度高度与原图相同。为32位ARGB图片

Bitmap currentBitmap = Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(), Bitmap.Config.ARGB_8888);

//创建一个和原图相同大小的画布

Canvas canvas = new Canvas(currentBitmap);

//创建笔刷并设置抗锯齿

Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

//色相ColorMatrix

ColorMatrix hueMatrix = new ColorMatrix();

hueMatrix.setRotate(0,hue);

hueMatrix.setRotate(1,hue);

hueMatrix.setRotate(2,hue);

//饱和度ColorMatrix

ColorMatrix saturationMatrix = new ColorMatrix();

saturationMatrix.setSaturation(saturation);

//亮度ColorMatrix

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(bitmap,0,0,paint);

return currentBitmap;

}

}

这里,用到了Canvas类,canvas类是一个画布类,后面进行的操作都会在画布上进行,而不是在原图上。建立了三个ColorMatrix,进行了不同方面的操作,并用postConcat方法将这些Matrix进行融合。通过setColorFilter方法来修改paint的相关属性,然后在canvas上将图片绘制出来。最后将修改后的图片返回出来。

回到我们的界面。设计是用三个Seekbar来分别控制色相、饱和度、亮度三个属性。

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android”

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:orientation=“vertical”>

<ImageView

android:id=“@+id/image_view”

android:layout_width=“300dp”

android:layout_height=“300dp”

android:scaleType=“centerCrop”

android:layout_marginTop=“24dp”

android:layout_marginBottom=“24dp”

android:layout_centerHorizontal=“true”/>

<SeekBar

android:id=“@+id/seekbar_hue”

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:layout_margin=“10dp”

android:layout_below=“@id/image_view”/>

<SeekBar

android:id=“@+id/seekbar_saturation”

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:layout_margin=“10dp”

android:layout_below=“@id/seekbar_hue”/>

<SeekBar

android:id=“@+id/seekbar_lum”

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:layout_margin=“10dp”

android:layout_below=“@id/seekbar_saturation”/>

然后编辑PrimaryColorActivity。这里我们定义了一个最大值及中间值,可以让它从中间值开始变化,然后通过一系列方法来分别获取色相、饱和度、亮度的值。

public cl

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值