对于我在java中的Android应用程序,我需要通过访问两张照片上的每个像素来混合2个图片,并获得像素的平均值以产生混合效果。我知道如何访问每个像素,但我不知道我计算混合平均值的方式是否正确。
这是我的代码:
import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView img;
private Bitmap bmp;
private ImageView img2;
private Bitmap bmp2;
private Bitmap operation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = (ImageView)findViewById(R.id.imageView1);
BitmapDrawable abmp = (BitmapDrawable)img.getDrawable();
bmp = abmp.getBitmap();
img2 = (ImageView)findViewById(R.id.imageView2);
BitmapDrawable abmp2 = (BitmapDrawable)img2.getDrawable();
bmp2 = abmp2.getBitmap();
}
public void blend(View v){
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 p2 = bmp2.getPixel(i, j);
int r = Color.red(p);
int g = Color.green(p);
int b = Color.blue(p);
int r2 = Color.red(p2);
int g2 = Color.green(p2);
int b2 = Color.blue(p2);
r=(int)(r+r2)/2;
g=(int)(g+g2)/2;
b=(int)(b+b2)/2;
img2.setVisibility(View.INVISIBLE);
operation.setPixel(i, j, Color.argb(Color.alpha(p), r, g, b));
}
}
img.setImageBitmap(operation);
}
布局如下:
<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: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.blendpics.MainActivity" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="114dp"
android:src="@drawable/pic1" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="114dp"
android:src="@drawable/pic2" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:onClick="blend"
android:text="blend" />
答案 0 :(得分:0)
看一下使用RenderScript和内置的内在函数,即ScriptIntrinsicBlend:http://developer.android.com/reference/android/renderscript/ScriptIntrinsicBlend.html。