Thứ Sáu, 28 tháng 9, 2018

Android - Hiệu ứng hình ảnh

Học lập trình Android cho phép bạn thao tác hình ảnh bằng cách thêm các loại hiệu ứng khác nhau vào hình ảnh. Bạn có thể dễ dàng áp dụng các kỹ thuật xử lý hình ảnh để thêm một số loại hiệu ứng nhất định trên hình ảnh. Các hiệu ứng có thể là độ sáng, bóng tối, chuyển đổi thang độ xám vv

Android cung cấp lớp Bitmap để xử lý hình ảnh. Điều này có thể được tìm thấy dưới Android. graphics.bitmap. Có nhiều cách để bạn có thể tạo bitmap. Chúng tôi đang tạo một bitmap của hình ảnh 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 của nó được đưa ra dưới đây
bmp = abmp.getBitmap();
Một hình ảnh không là gì ngoài ma trận hai chiều. Cùng một cách bạn sẽ xử lý một bitmap. Một hình ảnh bao gồm các điểm ảnh. Vì vậy, bạn sẽ nhận được điểm ảnh từ bitmap này và áp dụng chế biến cho nó. Cú pháp của nó như sau:
for(int i=0; i<bmp.getWidth(); i++){
   for(int j=0; j<bmp.getHeight(); j++){
      int p = bmp.getPixel(i, j);
   }
}

Hàm getWidth () và getHeight () trả về chiều cao và chiều rộng của ma trận. Phương thức getPixel () trả về pixel tại chỉ mục đã chỉ định. Một khi bạn đã có điểm ảnh, bạn có thể dễ dàng thao tác nó theo nhu cầu của bạn.

Ngoài các phương pháp này, còn có các phương pháp khác giúp chúng ta thao tác hình ảnh tốt hơn.

Sr.NoPhương pháp & mô tả
1copy (cấu hình Bitmap.Config, boolean isMutable)

Phương pháp này sao chép các pixel của bitmap này vào bitmap mới
2createBitmap (Hiển thị DisplayMetrics, chiều rộng int, chiều cao int, cấu hình Bitmap.Config)
Trả về bitmap có thể thay đổi với chiều rộng và chiều cao được chỉ định
3createBitmap (chiều rộng int, chiều cao int, cấu hình Bitmap.Config)

Trả về bitmap có thể thay đổi với chiều rộng và chiều cao được chỉ định
4createBitmap (bitmap src)

Trả về bitmap bất biến từ bitmap nguồn
5extractAlpha ()
Trả về một bitmap mới thu được các giá trị alpha của bản gốc
6getConfig ()

Mehtod này sẽ cấu hình lại, nếu không trả về null
7getDensity ()
Trả về mật độ cho bitmap này
số 8getRowBytes ()
Trả về số byte giữa các hàng trong pixel của bitmap
9setPixel (int x, int y, int color)

Viết màu được chỉ định vào bitmap (giả sử nó có thể thay đổi) tại toạ độ x, y
10setDensity (mật độ int)

Phương thức này chỉ định mật độ cho bitmap này

Thí dụ

Ví dụ dưới đây minh họa một số hiệu ứng hình ảnh trên bitmap. Nó thùng một ứng dụng cơ bản cho phép bạn chuyển đổi hình ảnh thành màu xám và nhiều hơn nữa.

Để thử nghiệm với ví dụ này, bạn cần chạy trên một thiết bị thực tế.

Các bướcSự miêu tả
1Bạn sẽ sử dụng Android studio để tạo ứng dụng Android theo gói com.example.sairamkrishna.myapplication.
2Sửa đổi tệp src / MainActivity.java để thêm mã cần thiết.
3Sửa đổi res / layout / activity_main để thêm các thành phần XML tương ứng
4Chạy ứng dụng và chọn thiết bị Android đang chạy và cài đặt ứng dụng trên đó và xác minh kết quả
Sau đây là nội dung của MainActivity.java đã sửa đổi .
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);
   }
}
Sau đây là nội dung sửa đổi của xml res / layout / activity_main.xml .

Ở đây abc cho biết về logo của tutorialspoint.com
<?xml version="1.0" encoding="utf-8"?>
<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/abc"/>
   
   <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>
Sau đây là nội dung của tệp AndroidManifest.xml .
<?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>
Hãy thử chạy ứng dụng của chúng tôi, chúng tôi vừa sửa đổi. Tôi cho rằng bạn đã tạo AVD của mình trong khi thiết lập môi trường.

Để chạy ứng dụng từ Android studio, hãy mở một trong các tệp hoạt động của dự án của bạn và nhấp vào biểu tượng Chạy từ thanh công cụ.

Android studio cài đặt ứng dụng trên AVD của bạn và khởi động ứng dụng và nếu mọi thứ đều ổn với thiết lập và ứng dụng của bạn, ứng dụng sẽ hiển thị cửa sổ Trình mô phỏng sau

Học lập trình Android

Bây giờ nếu bạn sẽ nhìn vào màn hình thiết bị của bạn, bạn sẽ thấy một hình ảnh của Android cùng với ba nút.

Bây giờ chỉ cần chọn nút màu xám sẽ chuyển đổi hình ảnh của bạn thành màu xám và sẽ cập nhật giao diện người dùng. Nó được hiển thị dưới đây

Bây giờ hãy nhấn vào nút sáng, điều đó sẽ thêm một số giá trị cho từng pixel của hình ảnh và do đó tạo ra ảo ảnh về độ sáng. Nó được hiển thị dưới đây

Bây giờ bấm vào nút tối, sẽ trừ một số giá trị cho mỗi điểm ảnh của hình ảnh và do đó làm cho một ảo ảnh của bóng tối. Nó được hiển thị dưới đây

Bây giờ hãy nhấn vào nút màu đỏ, điều đó sẽ trừ đi một số giá trị cho mỗi pixel của hình ảnh và do đó làm cho ảo ảnh của bóng tối. Nó được hiển thị dưới đây

Bây giờ bấm vào nút màu xanh lá cây, mà sẽ trừ một số giá trị cho mỗi điểm ảnh của hình ảnh và do đó làm cho một ảo ảnh của bóng tối. Nó được hiển thị dưới đây

Bây giờ hãy nhấn vào nút màu xanh lam, điều này sẽ trừ đi một số giá trị cho từng pixel của hình ảnh và do đó tạo ra ảo tưởng về bóng tối. Nó được hiển thị dưới đây

Học lập trình Android

Lập trình Android - RenderScript

Trong chương này, chúng ta sẽ tìm hiểu về Android RenderScript. Thông thường các ứng dụng trên Android được thiết kế để tiêu thụ tài nguyên ...