Android图像编辑和处理之调整图像明亮度
04 Oct 2013ColorMatrix是一个4 x 5的矩阵,M' = M * CM
M' = (R', G', B', A') : 为结果颜色矩阵;
M = (R, G, B, A) : 为源颜色矩阵;
CM = (a11 a12 a13 a14 a15)
(a21 a22 a23 a24 a25)
(a31 a32 a33 a34 a35)
(a41 a42 a43 a44 a45) : 为转换矩阵(ColorMatrix);
ColorMatrix的前4列作用于R, G, B, A, 第五列作用与亮度值。
通过ColorMatrix 不仅可以调整亮度还可以调整各个颜色值。
使用ColorMatrix的基本步骤 :
- 定义一个ColorMatrix : ColorMatrix cm = new ColorMatrix()
- 设置我们需要的颜色转化矩阵 : cm.set(new float[]{})
- 给当前Paint设置ColorFilter : paint.setColorFilter(new ColorMatrixColorFilter(cm))
- 使用设置好的Paint绘图
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/ChoosePictureButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Choose Picture" />
<SeekBar
android:id="@+id/brightnessSB"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/ChoosePictureButton"
android:max="200"
android:progress="100" />
</RelativeLayout>
<ImageView
android:id="@+id/ChooseImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/AlteredImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
MainActivity.java:
public class MainActivity extends Activity implements OnClickListener {
private static final int REQUEST_CODE = 0;
ImageView chosenImageView;
ImageView alteredImageView;
Bitmap alteredBitmap;
Bitmap bmp;
Button choosePicture;
//亮度
float brightness;
//调节亮度的进度条
SeekBar brightnessSB;
Canvas canvas;
Paint paint;
ColorMatrix cm;
Matrix matrix;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
alteredImageView = (ImageView) findViewById(R.id.AlteredImageView);
chosenImageView = (ImageView) findViewById(R.id.ChooseImageView);
choosePicture = (Button) findViewById(R.id.ChoosePictureButton);
choosePicture.setOnClickListener(this);
//变换矩阵,可以通过这个矩阵做很多图像缩放、平移和旋转等操作
matrix = new Matrix();
//颜色矩阵
cm = new ColorMatrix();
//画笔
paint = new Paint();
paint.setColorFilter(new ColorMatrixColorFilter(cm));
brightnessSB = (SeekBar) findViewById(R.id.brightnessSB);
brightnessSB.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onProgressChanged(SeekBar seekBar,
int progress, boolean fromUser) {
if (null != bmp) {
//seekbar的范围为[0, 200]
//brightness的范围为[-100, 100]
brightness = (float) (progress - 100);
cm.set(new float[] { 1, 0, 0, 0, brightness,
0, 1, 0, 0, brightness,
0, 0,1, 0, brightness,
0, 0, 0, 1, 0 });
paint.setColorFilter(new ColorMatrixColorFilter(cm));
canvas.drawBitmap(bmp, matrix, paint);
alteredImageView.setImageBitmap(alteredBitmap);
} else {
brightnessSB.setProgress(100);
}
}
});
}
@Override
public void onClick(View v) {
//请求Android的gallery程序,选取sdcard中的照片
Intent choosePictureIntent = new Intent(Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(choosePictureIntent, REQUEST_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
brightnessSB.setProgress(100);
Uri imageFileUri = data.getData();
Display currentDisplay = getWindowManager().getDefaultDisplay();
int dw = currentDisplay.getWidth();
int dh = currentDisplay.getHeight() / 3 - 100;
try {
BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
bmpFactoryOptions.inJustDecodeBounds = true;
bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageFileUri), null, bmpFactoryOptions);
int heightRatio = (int) Math.ceil(bmpFactoryOptions.outHeight
/ (int) dh);
int widthRatio = (int) Math.ceil(bmpFactoryOptions.outWidth
/ (int) dw);
if (heightRatio > 1 && widthRatio > 1) {
if (heightRatio > widthRatio) {
bmpFactoryOptions.inSampleSize = heightRatio;
} else {
bmpFactoryOptions.inSampleSize = widthRatio;
}
}
bmpFactoryOptions.inJustDecodeBounds = false;
bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageFileUri), null, bmpFactoryOptions);
alteredBitmap = Bitmap.createBitmap(bmp.getWidth(),
bmp.getHeight(), bmp.getConfig());
canvas = new Canvas(alteredBitmap);
canvas.drawBitmap(bmp, matrix, paint);
alteredImageView.setImageBitmap(alteredBitmap);
chosenImageView.setImageBitmap(bmp);
} catch (Exception e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
}
}
git clone https://github.com/lnmcc/brightnessExample.git
ref: 《Android多媒体开发高级编程》