Set the rotation on a color axis of Bitmap with ColorMatrix

Example to set the rotation on a color axis by the specified values. axis=0 correspond to a rotation around the RED color axis=1 correspond to a rotation around the GREEN color axis=2 correspond to a rotation around the BLUE color.

rotation on a color axis of Bitmap with ColorMatrix

package com.example.androiddrawbitmap;

import java.io.FileNotFoundException;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class MainActivity extends Activity {

Button btnLoadImage;
ImageView imageResult;
SeekBar rotBar;
TextView rotText;

RadioGroup groupAxis;
RadioButton optAxisRed, optAxisGreen, optAxisBlue;

final int RQS_IMAGE1 = 1;

Uri source;
Bitmap bitmapMaster;
Canvas canvasMaster;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

btnLoadImage = (Button) findViewById(R.id.loadimage);
imageResult = (ImageView) findViewById(R.id.result);

btnLoadImage.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
Intent intent = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, RQS_IMAGE1);
}
});

rotText = (TextView) findViewById(R.id.textRot);
rotBar = (SeekBar) findViewById(R.id.rotbar);
rotBar.setOnSeekBarChangeListener(seekBarChangeListener);

groupAxis = (RadioGroup) findViewById(R.id.axisgroup);
optAxisRed = (RadioButton) findViewById(R.id.axisred);
optAxisGreen = (RadioButton) findViewById(R.id.axisgreen);
optAxisBlue = (RadioButton) findViewById(R.id.axisblue);
groupAxis.setOnCheckedChangeListener(groupAxisOnCheckedChangeListener);

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (resultCode == RESULT_OK) {
switch (requestCode) {
case RQS_IMAGE1:
source = data.getData();

try {
bitmapMaster = BitmapFactory
.decodeStream(getContentResolver().openInputStream(
source));

optAxisRed.setChecked(true);
rotBar.setProgress(0);

loadBitmapSat();

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

break;
}
}
}

OnSeekBarChangeListener seekBarChangeListener =
new OnSeekBarChangeListener() {

@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub

}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub

}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
loadBitmapSat();
}
};

OnCheckedChangeListener groupAxisOnCheckedChangeListener =
new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
loadBitmapSat();
}
};

private void loadBitmapSat() {
if (bitmapMaster != null) {

int progressRot = rotBar.getProgress();

float rotDegree = (float) progressRot;

if (optAxisRed.isChecked()) {
rotText.setText("setRotate: " + "Red: "
+ String.valueOf(rotDegree));
imageResult
.setImageBitmap(updateRot(bitmapMaster, 0, rotDegree));
} else if (optAxisGreen.isChecked()) {
rotText.setText("setRotate: " + "Green: "
+ String.valueOf(rotDegree));
imageResult
.setImageBitmap(updateRot(bitmapMaster, 1, rotDegree));
} else if (optAxisBlue.isChecked()) {
rotText.setText("setRotate: " + "Blue: "
+ String.valueOf(rotDegree));
imageResult
.setImageBitmap(updateRot(bitmapMaster, 2, rotDegree));
}
}
}

private Bitmap updateRot(Bitmap src, int axis, float degrees) {

int w = src.getWidth();
int h = src.getHeight();

Bitmap bitmapResult = Bitmap
.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Canvas canvasResult = new Canvas(bitmapResult);
Paint paint = new Paint();
ColorMatrix colorMatrix = new ColorMatrix();
colorMatrix.setRotate(axis, degrees);
ColorMatrixColorFilter filter = new ColorMatrixColorFilter(colorMatrix);
paint.setColorFilter(filter);
canvasResult.drawBitmap(src, 0, 0, paint);

return bitmapResult;
}
}


<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:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="http://arteluzevida.blogspot.com/"
android:textStyle="bold" />

<Button
android:id="@+id/loadimage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Load Image 1" />

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >

<ImageView
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:background="@android:color/background_dark"
android:scaleType="centerInside" />

<TextView
android:id="@+id/textRot"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Rotate Axis" />

<RadioGroup
android:id="@+id/axisgroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<RadioButton
android:id="@+id/axisred"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:checked="true"
android:text="Red" />

<RadioButton
android:id="@+id/axisgreen"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Green" />

<RadioButton
android:id="@+id/axisblue"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Blue" />
</RadioGroup>

<SeekBar
android:id="@+id/rotbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="360"
android:progress="0" />
</LinearLayout>
</ScrollView>

</LinearLayout>


download filesDownload the files.

download filesDownload and try the APK.

Related: Combine ColorMatrixes by calling setConcat().


more: Something about processing images in Android