/**
* 图像合成
*
* @time 下午06:21:22
* @author retacn yue
* @Email zhenhuayue@sina.com
*/
public class ChosePictureCompositeActivity extends Activity implements OnClickListener {
public static final int PICKED_ONE = 0;
public static final int PICKED_TWO = 1;
private Button btn_choose_picture1;
private Button btn_choose_picture2;
private ImageView img_choose_picture;
private Bitmap bitmap1, bitmap2;
private Options bmpOptions;
private Canvas canvas;
private Paint paint;
private boolean onePicked = false;
private boolean twoPicked = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.chose_picture_composite);
findView();
}
private void findView() {
btn_choose_picture1 = (Button) this.findViewById(R.id.btn_choose_picture_composite1);
btn_choose_picture1.setOnClickListener(this);
btn_choose_picture2 = (Button) this.findViewById(R.id.btn_choose_picture_composite2);
btn_choose_picture2.setOnClickListener(this);
img_choose_picture = (ImageView) this.findViewById(R.id.img_choose_picture_composite);
}
@Override
public void onClick(View v) {
int witch = -1;
if (v == btn_choose_picture1) {
witch = PICKED_ONE;
} else if (v == btn_choose_picture2) {
witch = PICKED_TWO;
}
Intent intent = new Intent(Intent.ACTION_PICK, Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, witch);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
Uri imageFileUri = data.getData();
if (requestCode == PICKED_ONE) {
bitmap1 = loadBitmap(imageFileUri);
onePicked = true;
} else if (requestCode == PICKED_TWO) {
bitmap2 = loadBitmap(imageFileUri);
twoPicked = true;
}
if (onePicked && twoPicked) {
Log.i("tag", "=====choose is over!");
Bitmap drawingBitmap = Bitmap.createBitmap(bitmap1.getWidth(), bitmap1.getHeight(), bitmap1.getConfig());
canvas = new Canvas(drawingBitmap);
paint = new Paint();
canvas.drawBitmap(bitmap1, 0, 0, paint);
// 图像重叠的绘制规则 PorterDuffXfermode
// 常量:
// SRC 只绘制原图像:
// DST 只绘制目标图像:
// 另外和四个规则,在一幅图像上放置另一幅图像,如何合成
// LIGHT: 获得每个位置上两幅图像中最亮的像素并显示
// DARKEN:最暗的像素并显示
// MULTIPLY:将第个位置的像素相乘,除以255,创建一个新像素显示
// SCREEN: 反转每个颜色,执行如(MULTIPLY)常量的操作
paint.setXfermode(new PorterDuffXfermode(Mode.SCREEN));
canvas.drawBitmap(bitmap2, 0, 0, paint);
img_choose_picture.setImageBitmap(drawingBitmap);
}
}
}
/**
* 加载位图图像
*/
private Bitmap loadBitmap(Uri imageFileUri) {
// 取得屏幕大小
Display display = getWindowManager().getDefaultDisplay();
int dw = display.getWidth();
int dh = display.getHeight();
// 期望是ARGB_4444
Bitmap bitmap = Bitmap.createBitmap((int) dw, (int) dh, Bitmap.Config.ARGB_4444);
try {
// 加载图像的尺寸而不是图像本身
bmpOptions = new BitmapFactory.Options();
// 如果为true,只须返回图像的范围,不须尝试解码图像本身
bmpOptions.inJustDecodeBounds = true;
bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageFileUri), null, bmpOptions);
int hRatio = (int) Math.ceil(bmpOptions.outHeight / (float) dh);
int wRatio = (int) Math.ceil(bmpOptions.outWidth / (float) dw);
Log.i("tag", "hRatio" + hRatio);
Log.i("tag", "wRatio" + wRatio);
// 如果两个比率都大于1
// 那么图像的一条边将大小屏幕
if (hRatio > 1 && wRatio > 1) {
if (hRatio > wRatio) {
Log.i("tag", "hRatio" + hRatio);
// 若高度比率更大,则根据它缩放
bmpOptions.inSampleSize = hRatio;
} else {
Log.i("tag", "wRatio" + wRatio);
// 反之则根据宽度缩放
bmpOptions.inSampleSize = wRatio;
}
}
// 对图像进行解码
bmpOptions.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageFileUri), null, bmpOptions);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return bitmap;
* 图像合成
*
* @time 下午06:21:22
* @author retacn yue
* @Email zhenhuayue@sina.com
*/
public class ChosePictureCompositeActivity extends Activity implements OnClickListener {
public static final int PICKED_ONE = 0;
public static final int PICKED_TWO = 1;
private Button btn_choose_picture1;
private Button btn_choose_picture2;
private ImageView img_choose_picture;
private Bitmap bitmap1, bitmap2;
private Options bmpOptions;
private Canvas canvas;
private Paint paint;
private boolean onePicked = false;
private boolean twoPicked = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.chose_picture_composite);
findView();
}
private void findView() {
btn_choose_picture1 = (Button) this.findViewById(R.id.btn_choose_picture_composite1);
btn_choose_picture1.setOnClickListener(this);
btn_choose_picture2 = (Button) this.findViewById(R.id.btn_choose_picture_composite2);
btn_choose_picture2.setOnClickListener(this);
img_choose_picture = (ImageView) this.findViewById(R.id.img_choose_picture_composite);
}
@Override
public void onClick(View v) {
int witch = -1;
if (v == btn_choose_picture1) {
witch = PICKED_ONE;
} else if (v == btn_choose_picture2) {
witch = PICKED_TWO;
}
Intent intent = new Intent(Intent.ACTION_PICK, Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, witch);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
Uri imageFileUri = data.getData();
if (requestCode == PICKED_ONE) {
bitmap1 = loadBitmap(imageFileUri);
onePicked = true;
} else if (requestCode == PICKED_TWO) {
bitmap2 = loadBitmap(imageFileUri);
twoPicked = true;
}
if (onePicked && twoPicked) {
Log.i("tag", "=====choose is over!");
Bitmap drawingBitmap = Bitmap.createBitmap(bitmap1.getWidth(), bitmap1.getHeight(), bitmap1.getConfig());
canvas = new Canvas(drawingBitmap);
paint = new Paint();
canvas.drawBitmap(bitmap1, 0, 0, paint);
// 图像重叠的绘制规则 PorterDuffXfermode
// 常量:
// SRC 只绘制原图像:
// DST 只绘制目标图像:
// 另外和四个规则,在一幅图像上放置另一幅图像,如何合成
// LIGHT: 获得每个位置上两幅图像中最亮的像素并显示
// DARKEN:最暗的像素并显示
// MULTIPLY:将第个位置的像素相乘,除以255,创建一个新像素显示
// SCREEN: 反转每个颜色,执行如(MULTIPLY)常量的操作
paint.setXfermode(new PorterDuffXfermode(Mode.SCREEN));
canvas.drawBitmap(bitmap2, 0, 0, paint);
img_choose_picture.setImageBitmap(drawingBitmap);
}
}
}
/**
* 加载位图图像
*/
private Bitmap loadBitmap(Uri imageFileUri) {
// 取得屏幕大小
Display display = getWindowManager().getDefaultDisplay();
int dw = display.getWidth();
int dh = display.getHeight();
// 期望是ARGB_4444
Bitmap bitmap = Bitmap.createBitmap((int) dw, (int) dh, Bitmap.Config.ARGB_4444);
try {
// 加载图像的尺寸而不是图像本身
bmpOptions = new BitmapFactory.Options();
// 如果为true,只须返回图像的范围,不须尝试解码图像本身
bmpOptions.inJustDecodeBounds = true;
bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageFileUri), null, bmpOptions);
int hRatio = (int) Math.ceil(bmpOptions.outHeight / (float) dh);
int wRatio = (int) Math.ceil(bmpOptions.outWidth / (float) dw);
Log.i("tag", "hRatio" + hRatio);
Log.i("tag", "wRatio" + wRatio);
// 如果两个比率都大于1
// 那么图像的一条边将大小屏幕
if (hRatio > 1 && wRatio > 1) {
if (hRatio > wRatio) {
Log.i("tag", "hRatio" + hRatio);
// 若高度比率更大,则根据它缩放
bmpOptions.inSampleSize = hRatio;
} else {
Log.i("tag", "wRatio" + wRatio);
// 反之则根据宽度缩放
bmpOptions.inSampleSize = wRatio;
}
}
// 对图像进行解码
bmpOptions.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageFileUri), null, bmpOptions);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return bitmap;
}
}