Android-画像効果
Androidでは、画像にさまざまな種類の効果を追加して画像を操作できます。 画像処理技術を簡単に適用して、画像に特定の種類の効果を追加できます。 効果は、明るさ、暗さ、グレースケール変換などです。
Androidは、画像を処理するBitmapクラスを提供します。 これはandroid.graphics.bitmapにあります。 ビットマップをインスタンス化する方法はたくさんあります。 imageViewから画像のビットマップを作成しています。
次に、BitmapDrawableクラスのgetBitmap()関数を呼び出してビットマップを作成します。 その構文は以下のとおりです-
画像は2次元のマトリックスにすぎません。 ビットマップを処理するのと同じ方法。 画像はピクセルで構成されます。 したがって、このビットマップからピクセルを取得し、それに処理を適用します。 その構文は次のとおりです-
getWidth()およびgetHeight()関数は、マトリックスの高さと幅を返します。 getPixel()メソッドは、指定されたインデックスのピクセルを返します。 ピクセルを取得したら、必要に応じて簡単に操作できます。
これらの方法とは別に、画像をより良く操作するのに役立つ他の方法があります。
Sr.No
|
Method & description
|
1
|
copy(Bitmap.Config config, boolean isMutable)
このメソッドは、このビットマップのピクセルを新しいビットマップにコピーします
|
2
|
createBitmap(DisplayMetrics display, int width, int height, Bitmap.Config config)
指定された幅と高さで可変ビットマップを返します
|
3
|
createBitmap(int width, int height, Bitmap.Config config)
指定された幅と高さで可変ビットマップを返します
|
4
|
createBitmap(Bitmap src)
ソースビットマップから不変ビットマップを返します
|
5
|
extractAlpha()
元のアルファ値をキャプチャする新しいビットマップを返します
|
6
|
getConfig()
この方法はその設定を返し、そうでない場合はnullを返します
|
7
|
getDensity()
このビットマップの密度を返します
|
8
|
getRowBytes()
ビットマップのピクセルの行間のバイト数を返します
|
9
|
setPixel(int x, int y, int color)
x、y座標で、指定されたColorをビットマップに書き込みます(可変であると想定)
|
10
|
setDensity(int density)
このメソッドは、このビットマップの密度を指定します
|
例
以下の例は、ビットマップの画像効果の一部を示しています。 画像をグレースケールなどに変換できる基本的なアプリケーションを作成します。
この例を試すには、実際のデバイスでこれを実行する必要があります。
Steps
|
Description
|
1
|
You will use Android studio to create an Android application under a package com.example.sairamkrishna.myapplication.
|
2
|
Modify src/MainActivity.java file to add necessary code.
|
3
|
Modify the res/layout/activity_main to add respective XML components
|
4
|
Run the application and choose a running android device and install the application on it and verify the results
|
変更された 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);
}
}
以下は、xml* res/layout/activity_main.xml *の変更されたコンテンツです。
'_ここでabcはfinddevguides.comのロゴについて示しています_
以下は AndroidManifest.xml ファイルの内容です。
変更したばかりのアプリケーションを実行してみましょう。 環境設定中に AVD を作成したと思います。 Androidスタジオからアプリを実行するには、プロジェクトのアクティビティファイルの1つを開き、ツールバーの[画像を実行:/android/images/eclipse_run.jpg [Eclipse Run Icon]アイコンをクリックします。 AndroidスタジオはAVDにアプリをインストールして起動し、セットアップとアプリケーションで問題がなければ、次のエミュレータウィンドウが表示されます-
Android Image Effects Tutorial
デバイス画面を見ると、3つのボタンとともにAndroidの画像が表示されます。
画像をグレースケールに変換してUIを更新する[グレー]ボタンを選択します。 以下に示されています-
Android Image Effects Tutorial
明るいボタンをタップすると、画像の各ピクセルに値が追加され、明るさの錯覚が生まれます。 以下に示されています-
Anroid Image Effects Tutorial
次に、暗いボタンをタップします。これにより、画像の各ピクセルに値が差し引かれ、暗くなります。 以下に示されています-
Android Image Effects Tutorial
次に、赤いボタンをタップします。これにより、画像の各ピクセルに値が差し引かれ、暗くなります。 以下に示されています-
Android Image Effects Tutorial
次に、緑色のボタンをタップします。これにより、画像の各ピクセルに値が差し引かれ、暗くなります。 以下に示されています-
Android Image Effects Tutorial
青いボタンをタップすると、画像の各ピクセルに値が差し引かれ、暗闇のように見えます。 以下に示されています-
Android Image Effects Tutorial