Image Effect trong Android



Android cho phép bạn thao tác với hình ảnh bằng cách thêm các kiểu hiệu ứng khác nhau trên các hình ảnh. Bạn có thể dễ dàng áp dụng các công nghệ xử lý ảnh để thêm một số kiểu hiệu ứng khác nhau trên ảnh. Hiệu ứng có thể là làm sáng, làm mờ, …

Android cung cấp lớp Bitmap để xử lý hình ảnh. Lớp này có thể được tìm thấy dưới android.graphics.bitmap. Có nhiều cách thông qua đó bạn có thể khởi tạo Bitmap. Chúng ta đang tạo một Bitmap từ imageView:

private Bitmap bmp;
private ImageView img;
img = (ImageView)findViewById(R.id.imageView1);
BitmapDrawable  abmp = (BitmapDrawable)img.getDrawable();

Bây giờ chúng ta sẽ tạo Bitmap bằng cách gọi hàm getBitmap() của lớp BitmapDrawable. Cú pháp là:−

bmp = abmp.getBitmap();

Một hình ảnh là không gì khác ngoài một ma trận hai chiều. Tương tự như thế bạn sẽ xử lý một Bitmap. Một hình ảnh gồm các pixel, vì thế bạn sẽ lấy pixel từ Bitmap này và áp dụng tiến trình xử lý tới nó. Cú pháp là: −

for(int i=0; i<bmp.getWidth(); i++){
   for(int j=0; j<bmp.getHeight(); j++){
      int p = bmp.getPixel(i, j);
   }
}
Quảng cáo

Các hàm getWidth() và getHeight() trả về độ rộng và chiều cao của ma trận. Phương thức getPixel() trả về pixel tại chỉ mục đã xác định. Khi bạn đã lấy được pixel, bạn có thể dễ dàng thao tác nó tùy theo bạn.

Ngoài các phương thức này, bảng dưới liệt kê một số phương thức khác giúp chúng ta thao tác với hình ảnh tốt hơn.

Stt Phương thức & Miêu tả
1 copy(Bitmap.Config config, boolean isMutable)

Phương thức này sao chép pixel của Bitmap này vào trong Bitmap mới

2 createBitmap(DisplayMetrics display, int width, int height, Bitmap.Config config)

Phương thức này trả về một Mutable Bitmap (Bitmap có thể thay đổi) với độ rộng và chiều cao đã xác định

3 createBitmap(int width, int height, Bitmap.Config config)

Trả về một Mutable Bitmap với độ rộng và chiều cao đã xác định

4 createBitmap(Bitmap src)

Trả về một Immutable Bitmap (Bitmap không thể biến đổi) từ Source Bitmap

5 extractAlpha()

Trả về một Bitmap mới mà bắt các giá trị alpha của bitmap ban đầu

6 getConfig()

Phương thức này trả về cấu hình đó, nếu không trả về null

7 getDensity()

Trả về mật độ cho Bitmap này

8 getRowBytes()

Trả về số byte giữa các hàng trong pixel của Bitmap

9 setPixel(int x, int y, int color)

Ghi màu đã cho vào trong Bitmap (giả sử nó là có thể biến đổi) tại tọa độ x, y

10 setDensity(int density)

Phương thức này xác định mật độ cho Bitmap này

Ví dụ

Sau đây là các bước tạo Image Effect cho ứng dụng Android.

Và đây là nội dung của modified MainActivity.java.

package com.example.sairamkrishna.myapplication;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends ActionBarActivity {
   Button b1, b2, b3;
   ImageView im;
   
   private Bitmap bmp;
   private Bitmap operation;
   
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      
      b1 = (Button) findViewById(R.id.button);
      b2 = (Button) findViewById(R.id.button2);
      b3 = (Button) findViewById(R.id.button3);
      im = (ImageView) findViewById(R.id.imageView);
      
      BitmapDrawable abmp = (BitmapDrawable) im.getDrawable();
      bmp = abmp.getBitmap();
   }
   
   public void gray(View view) {
      operation = Bitmap.createBitmap(bmp.getWidth(),bmp.getHeight(), bmp.getConfig());
      double red = 0.33;
      double green = 0.59;
      double blue = 0.11;
      
      for (int i = 0; i < bmp.getWidth(); i++) {
         for (int j = 0; j < bmp.getHeight(); j++) {
            int p = bmp.getPixel(i, j);
            int r = Color.red(p);
            int g = Color.green(p);
            int b = Color.blue(p);
            
            r = (int) red * r;
            g = (int) green * g;
            b = (int) blue * b;
            operation.setPixel(i, j, Color.argb(Color.alpha(p), r, g, b));
         }
      }
      im.setImageBitmap(operation);
   }
   
   public void bright(View view){
      operation= Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(),bmp.getConfig());
      
      for(int i=0; i<bmp.getWidth(); i++){
         for(int j=0; j<bmp.getHeight(); j++){
            int p = bmp.getPixel(i, j);
            int r = Color.red(p);
            int g = Color.green(p);
            int b = Color.blue(p);
            int alpha = Color.alpha(p);
            
            r = 100  +  r;
            g = 100  + g;
            b = 100  + b;
            alpha = 100 + alpha;
            operation.setPixel(i, j, Color.argb(alpha, r, g, b));
         }
      }
      im.setImageBitmap(operation);
   }
   
   public void dark(View view){
      operation= Bitmap.createBitmap(bmp.getWidth(),bmp.getHeight(),bmp.getConfig());
      
      for(int i=0; i<bmp.getWidth(); i++){
         for(int j=0; j<bmp.getHeight(); j++){
            int p = bmp.getPixel(i, j);
            int r = Color.red(p);
            int g = Color.green(p);
            int b = Color.blue(p);
            int alpha = Color.alpha(p);
            
            r =  r - 50;
            g =  g - 50;
            b =  b - 50;
            alpha = alpha -50;
            operation.setPixel(i, j, Color.argb(Color.alpha(p), r, g, b));
         }
      }
      im.setImageBitmap(operation);
   }
   
   public void gama(View view) {
      operation = Bitmap.createBitmap(bmp.getWidth(),bmp.getHeight(),bmp.getConfig());
      
      for(int i=0; i<bmp.getWidth(); i++){
         for(int j=0; j<bmp.getHeight(); j++){
            int p = bmp.getPixel(i, j);
            int r = Color.red(p);
            int g = Color.green(p);
            int b = Color.blue(p);
            int alpha = Color.alpha(p);
            
            r =  r + 150;
            g =  0;
            b =  0;
            alpha = 0;
            operation.setPixel(i, j, Color.argb(Color.alpha(p), r, g, b));
         }
      }
      im.setImageBitmap(operation);
   }
   
   public void green(View view){
      operation = Bitmap.createBitmap(bmp.getWidth(),bmp.getHeight(), bmp.getConfig());
      
      for(int i=0; <bmp.getWidth(); i++){
         for(int j=0; j<bmp.getHeight(); j++){
            int p = bmp.getPixel(i, j);
            int r = Color.red(p);
            int g = Color.green(p);
            int b = Color.blue(p);
            int alpha = Color.alpha(p);
            
            r =  0;
            g =  g+150;
            b =  0;
            alpha = 0;
            operation.setPixel(i, j, Color.argb(Color.alpha(p), r, g, b));
         }
      }
      im.setImageBitmap(operation);
   }
   
   public void blue(View view){
      operation = Bitmap.createBitmap(bmp.getWidth(),bmp.getHeight(), bmp.getConfig());
      
      for(int i=0; i<bmp.getWidth(); i++){
         for(int j=0; j<bmp.getHeight(); j++){
            int p = bmp.getPixel(i, j);
            int r = Color.red(p);
            int g = Color.green(p);
            int b = Color.blue(p);
            int alpha = Color.alpha(p);
            
            r =  0;
            g =  0;
            b =  b+150;
            alpha = 0;
            operation.setPixel(i, j, Color.argb(Color.alpha(p), r, g, b));
         }
      }
      im.setImageBitmap(operation);
   }
}

Bạn sửa đổi nội dung của res/layout/activity_main.xml.

Quảng cáo
<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
   
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/textView"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true"
      android:textSize="30dp"
      android:text="Image Effects" />
      
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Tutorials Point"
      android:id="@+id/textView2"
      android:layout_below="@+id/textView"
      android:layout_centerHorizontal="true"
      android:textSize="35dp"
      android:textColor="#ff16ff01" />
      
   <ImageView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/imageView"
      android:layout_below="@+id/textView2"
      android:layout_centerHorizontal="true"
      android:src="@drawable/logo"/>
   
   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Gray"
      android:onClick="gray"
      android:id="@+id/button"
      android:layout_alignParentBottom="true"
      android:layout_alignParentLeft="true"
      android:layout_alignParentStart="true"
      android:layout_marginBottom="97dp" />
      
   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="dark"
      android:onClick="dark"
      android:id="@+id/button2"
      android:layout_alignBottom="@+id/button"
      android:layout_alignParentRight="true"
      android:layout_alignParentEnd="true" />
      
   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Bright"
      android:onClick="bright"
      android:id="@+id/button3"
      android:layout_alignTop="@+id/button2"
      android:layout_centerHorizontal="true" />
      
   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Red"
      android:onClick="gama"
      android:id="@+id/button4"
      android:layout_below="@+id/button3"
      android:layout_alignParentLeft="true"
      android:layout_alignParentStart="true" />
      
   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Green"
      android:onClick="green"
      android:id="@+id/button5"
      android:layout_alignTop="@+id/button4"
      android:layout_alignLeft="@+id/button3"
      android:layout_alignStart="@+id/button3" />
      
   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="blue"
      android:onClick="blue"
      android:id="@+id/button6"
      android:layout_below="@+id/button2"
      android:layout_toRightOf="@+id/textView"
      android:layout_toEndOf="@+id/textView" />
      
</RelativeLayout>

Tiếp theo là nội dung của AndroidManifest.xml file.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.sairamkrishna.myapplication" >
   <application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" >
   
      <activity
         android:name=".MainActivity"
         android:label="@string/app_name" >
      
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
         
      </activity>
      
   </application>
</manifest>

Cuối cùng, bạn chạy ứng dụng Android vừa sửa đổi ở trên. Giả sử bạn đã tạo AVD trong khi cài đặt. Để chạy ứng dụng từ Android Studio, mở activity file và nhấn biểu tượng Run từ thanh công cụ. −

Đã có app VietJack trên điện thoại, giải bài tập SGK, SBT Soạn văn, Văn mẫu, Thi online, Bài giảng....miễn phí. Tải ngay ứng dụng trên Android và iOS.

Theo dõi chúng tôi miễn phí trên mạng xã hội facebook và youtube:

Các bạn có thể mua thêm khóa học JAVA CORE ONLINE VÀ ỨNG DỤNG cực hay, giúp các bạn vượt qua các dự án trên trường và đi thực tập Java. Khóa học có giá chỉ 300K, nhằm ưu đãi, tạo điều kiện cho sinh viên cho thể mua khóa học.

Nội dung khóa học gồm 16 chuơng và 100 video cực hay, học trực tiếp tại https://www.udemy.com/tu-tin-di-lam-voi-kien-thuc-ve-java-core-toan-tap/ Bạn nào có nhu cầu mua, inbox trực tiếp a Tuyền, cựu sinh viên Bách Khoa K53, fb: https://www.facebook.com/tuyen.vietjack

Follow facebook cá nhân Nguyễn Thanh Tuyền https://www.facebook.com/tuyen.vietjack để tiếp tục theo dõi các loạt bài mới nhất về Java,C,C++,Javascript,HTML,Python,Database,Mobile.... mới nhất của chúng tôi.




Tài liệu giáo viên