1. Trang chủ
  2. » Thể loại khác

image effect trong android

5 132 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 5
Dung lượng 232,43 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

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 để

Trang 1

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( 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= ; i<bmp.getWidth(); i++){ for(int j= ; j<bmp.getHeight(); j++){ int p = bmp.getPixel( , j); } }

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

Trang 2

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

Trang 3

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( layout.activity_main); b1 = (Button)

findViewById( id.button); b2 = (Button) findViewById( id.button2);

b3 = (Button) findViewById( id.button3); im = (ImageView)

findViewById( 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( , j); int

r = Color.red( ); int g = Color.green( ); int b =

Color.blue( ); r = (int) red * r; g =

(int) green * g; b = (int) blue * b;

operation.setPixel( , j, Color.argb(Color.alpha( ), r, g, b)); }

} im.setImageBitmap(operation); } public void bright(View

view){ operation= Bitmap.createBitmap(bmp.getWidth(),

bmp.getHeight(),bmp.getConfig()); for(int i= ; i<bmp.getWidth();

i++){ for(int j= ; j<bmp.getHeight(); j++){ int p =

bmp.getPixel( , j); int r = Color.red( ); int g =

Color.green( ); int b = Color.blue( ); int alpha =

Color.alpha( ); r = 100 + r; g = 100 +

g b = 100 + b; alpha = 100 alpha;

operation.setPixel( , 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= ; i<bmp.getWidth(); i++){ for(int j= ; j<bmp.getHeight();

j++){ int p = bmp.getPixel( , j); int r = Color.red( ); int g = Color.green( ); int b = Color.blue( ); int

alpha = Color.alpha( ); r = r - 50; g =

g - 50; b = b - 50; alpha = alpha -50;

operation.setPixel( , j, Color.argb(Color.alpha( ), r, g, b)); }

} im.setImageBitmap(operation); } public void gama(View view) {

operation =

Bitmap.createBitmap(bmp.getWidth(),bmp.getHeight(),bmp.getConfig());

for(int i= ; i<bmp.getWidth(); i++){ for(int j= ; j<bmp.getHeight();

j++){ int p = bmp.getPixel( , j); int r = Color.red( ); int g = Color.green( ); int b = Color.blue( ); int

alpha = Color.alpha( ); r = r + 150; g =

0 b = 0 alpha = 0 operation.setPixel( ,

j Color.argb(Color.alpha( ), r, g, b)); } }

im.setImageBitmap(operation); } public void green(View view){

operation = Bitmap.createBitmap(bmp.getWidth(),bmp.getHeight(),

Trang 4

bmp.getConfig()); for(int i= ; <bmp.getWidth(); i++){

for(int j= ; j<bmp.getHeight(); j++){ int p = bmp.getPixel( , j); int r = Color.red( ); int g = Color.green( ); int b =

Color.blue( ); int alpha = Color.alpha( );

r = 0 g = g+150; b = 0 alpha = 0 operation.setPixel( , j, Color.argb(Color.alpha( ), r, g, b)); }

} im.setImageBitmap(operation); } public void blue(View view){ operation = Bitmap.createBitmap(bmp.getWidth(),bmp.getHeight(),

bmp.getConfig()); for(int i= ; i<bmp.getWidth(); i++){

for(int j= ; j<bmp.getHeight(); j++){ int p = bmp.getPixel( , j); int r = Color.red( ); int g = Color.green( ); int b =

Color.blue( ); int alpha = Color.alpha( );

r = 0 g = 0 b = b+150; alpha = 0 operation.setPixel( , j, Color.argb(Color.alpha( ), r, g, b)); }

} im.setImageBitmap(operation); } }

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

<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"

Trang 5

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ụ −

Ngày đăng: 02/12/2017, 07:08

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

  • Đang cập nhật ...

TÀI LIỆU LIÊN QUAN