Monday, February 23, 2015

Create Sepia bitmap using ColorMatrix

Example to create spedia bitmap uwing ColorMatrix.


package com.example.androidimageview;

import android.support.v7.app.ActionBarActivity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

 TextView textTitle;
 ImageView image1, image2, image3;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  textTitle = (TextView) findViewById(R.id.title);
  image1 = (ImageView) findViewById(R.id.image1);
  image2 = (ImageView) findViewById(R.id.image2);
  image3 = (ImageView) findViewById(R.id.image3);

  Bitmap bm = BitmapFactory.decodeResource(getResources(),
    R.drawable.ic_launcher);

  image1.setImageBitmap(bm);
  image2.setImageBitmap(createSepia(bm));
  image3.setImageBitmap(createSepia(bm));

 }

 private Bitmap createSepia(Bitmap src) {
  ColorMatrix colorMatrix_Sepia = new ColorMatrix();
  colorMatrix_Sepia.setSaturation(0);
  
  ColorMatrix colorScale = new ColorMatrix();
  colorScale.setScale(1, 1, 0.8f, 1);
  
  colorMatrix_Sepia.postConcat(colorScale);
  
  ColorFilter ColorFilter_Sepia = new ColorMatrixColorFilter(
    colorMatrix_Sepia);

  Bitmap bitmap = Bitmap.createBitmap(src.getWidth(), src.getHeight(),
    Bitmap.Config.ARGB_8888);
  Canvas canvas = new Canvas(bitmap);

  Paint paint = new Paint();

  paint.setColorFilter(ColorFilter_Sepia);
  canvas.drawBitmap(src, 0, 0, paint);

  return 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: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="com.example.androidimageview.MainActivity" >

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:text="http://android-er.blogspot.com/"
        android:textStyle="bold" />

    <ImageView
        android:id="@+id/image1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    
    <ImageView
        android:id="@+id/image2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <ImageView
        android:id="@+id/image3"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

No comments:

Post a Comment