Android图像处理1

时间:2021-11-13 09:54:32

  项目开发要用,在慕课中学习了一下关于Android图像处理的相关功能,并进行了整理。

  在Android中,我们通过最基本的改变图像的RGBA值,改变图像的颜色与饱和度。

  Android中有ColorMatrix这样的类来改变图像的RGBA值。通过ColorMatrix中的setRotate方法改变图像的色调,通过setStaturation方法改变图像的饱和度,通过setScale方法改变图像的亮度。

  下面以更改实例图片的RGBA参数进行图像的处理。

  设置一个Button

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="btpc"
android:text="PrimaryColor" />

  (1)为了实现处理图像的方法,我们将处理图像的方法封装起来,便于后面直接调用该方法

package com.example.image2;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint; public class ImageHelper {
public static Bitmap HandleImageEffect(Bitmap bm,float hue,float saturation,float lum){
/*传入的Bitmap默认是不可以修改的,因此这里使用createBitmap方法得到以个bm的复制*/
Bitmap bmp = Bitmap.createBitmap(bm.getWidth(),bm.getHeight(),Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bmp); //创建画布,并传入bmp Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
/*创建ColorMatrix对象,使用setRotate方法改变值,第一个参数为0时,改变R的值
* 为1时,改变G参数
* 为3时,改变B参数*/
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对象改变亮度 ,透明度1*/
ColorMatrix lumMatrix = new ColorMatrix();
lumMatrix.setScale(lum, lum, lum, 1); /*合并三种处理效果,同样创建colorMatrix对象,通过postConcat方法将
* 三种属性合并到新的画布上*/
ColorMatrix imageMatirx = new ColorMatrix();
imageMatirx.postConcat(hueMatrix);
imageMatirx.postConcat(saturationMatrix);
imageMatirx.postConcat(lumMatrix); /*通过Paint中的setColorFilter方法,将colorMatrix对象转变为color对象传递进去*/
paint.setColorFilter(new ColorMatrixColorFilter(imageMatirx));
canvas.drawBitmap(bm, 0, 0,paint); //绘制在画布上显示出来 return bmp; //返回处理后的图像
}
}

  (2)新建一个Intent进行处理图片的操作和显示界面。使用seekBar控制RGBA参数。

<?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/imageView1"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_margin="24dp"
android:layout_marginBottom="24dp"
android:layout_centerHorizontal="true" /> <SeekBar
android:id="@+id/seekBarHue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/imageView1" /> <SeekBar
android:id="@+id/seekBarSat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/seekBarHue" /> <SeekBar
android:id="@+id/seekBarlum"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/seekBarSat" /> </RelativeLayout>

  (3)在primaryColor类中实现参数的修改与传递

package com.example.image2;

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 PrimaryColor extends Activity implements
SeekBar.OnSeekBarChangeListener {
private ImageView mImageView;
private SeekBar mSeekabrhue, mSeekabrSat, mSeekBarlum;
private static int MAX_VALUE = 255;
private static int MID_VALUE = 127;
private float mHue, mSat, mLum; //处理时调整参数的临时变量
private Bitmap bitmap; //待处理的照片 @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.primarycolor);
/*从资源中取出图片转化为BitMap*/
bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_launcher);
/*获取seekBar*/
mImageView = (ImageView) findViewById(R.id.imageView1);
mSeekabrhue = (SeekBar) findViewById(R.id.seekBarHue);
mSeekabrSat = (SeekBar) findViewById(R.id.seekBarSat);
mSeekBarlum = (SeekBar) findViewById(R.id.seekBarlum);
/*设置三个seekBar的监听事件*/
mSeekabrhue.setOnSeekBarChangeListener(this);
mSeekabrSat.setOnSeekBarChangeListener(this);
mSeekBarlum.setOnSeekBarChangeListener(this); /*设置seekBar的最大值*/
mSeekabrhue.setMax(MAX_VALUE);
mSeekabrSat.setMax(MAX_VALUE);
mSeekBarlum.setMax(MAX_VALUE); /*初始时位于中间位置*/
mSeekabrhue.setProgress(MID_VALUE);
mSeekabrSat.setProgress(MID_VALUE);
mSeekBarlum.setProgress(MID_VALUE);
/*初始时为原图*/
mImageView.setImageBitmap(bitmap);
} /*对seekBar改变事件的监听*/
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean arg2) {
// TODO Auto-generated method stub
/*获取当前操作的seekBar,并对相应的值进行调整*/
switch (seekBar.getId()) {
case R.id.seekBarHue:
mHue = (progress - MID_VALUE) * 1.0F / MID_VALUE * 180;
break;
case R.id.seekBarSat:
mSat = progress * 1.0F / MID_VALUE;
break;
case R.id.seekBarlum:
mLum = progress * 1.0F / MID_VALUE;
break; }
/*调用ImageHelper中的HandleImageEffect方法处理图片
* 返回处理后的bitmap
* 更改imageView改变显示的图片*/
mImageView.setImageBitmap(ImageHelper.HandleImageEffect(bitmap, mHue,
mSat, mLum));
} @Override
public void onStartTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub } @Override
public void onStopTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub }
}

  (4)最后需要在mainActivity中启动primaryColor类并且在配置文件中添加新建的activity。