- package demo.camera;
- import java.io.FileNotFoundException;
- import .Activity;
- import android.content.Intent;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.Canvas;
- import android.graphics.ColorMatrix;
- import android.graphics.ColorMatrixColorFilter;
- import android.graphics.Matrix;
- import android.graphics.Paint;
- import android.graphics.PorterDuff;
- import android.graphics.PorterDuffXfermode;
- import android.net.Uri;
- import android.os.Bundle;
- import android.provider.MediaStore;
- import android.util.Log;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.Button;
- import android.widget.ImageView;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public class PhotoProcess extends Activity{
- public static final int FIRST_PIC = 0;
- public static final int SECOND_PIC = 1;
- public static final int MAX_WIDTH = 240;
- public static final int MAX_HEIGHT = 180;
- private Button btnSelect,btnSelect2;
- private ImageView srcImageView, dstImageView;
-
- private Bitmap srcBitmap, dstBitmap;
- private Uri p_w_picpathUri;
-
-
- public void onCreate(Bundle savedInstanceState){
- super.onCreate(savedInstanceState);
- this.setContentView(R.layout.process);
-
- this.btnSelect = (Button)this.findViewById(.btn_select);
- btnSelect.setOnClickListener(clickListener);
- this.btnSelect2 = (Button)this.findViewById(.btn_select2);
- btnSelect2.setOnClickListener(clickListener2);
- srcImageView = (ImageView)this.findViewById(.img_src);
- dstImageView = (ImageView)this.findViewById(.img_dst);
- }
-
- private View.OnClickListener clickListener = new View.OnClickListener() {
-
- @Override
- public void onClick(View arg0) {
-
- Intent intent = new Intent(Intent.ACTION_PICK, .EXTERNAL_CONTENT_URI);
- startActivityForResult(intent, FIRST_PIC);
- }
- };
- private View.OnClickListener clickListener2 = new View.OnClickListener() {
-
- @Override
- public void onClick(View arg0) {
-
- Intent intent = new Intent(Intent.ACTION_PICK, .EXTERNAL_CONTENT_URI);
- startActivityForResult(intent, SECOND_PIC);
-
- }
- };
-
- public boolean onCreateOptionsMenu(Menu menu){
-
-
-
- menu.add(Menu.NONE,1,Menu.NONE,"复制");
- menu.add(Menu.NONE,2,Menu.NONE,"变换");
- menu.add(Menu.NONE,3,Menu.NONE,"亮度");
- menu.add(Menu.NONE,4,Menu.NONE,"合成");
- return super.onCreateOptionsMenu(menu);
- }
-
- public boolean onOptionsItemSelected(MenuItem item){
- int id = item.getItemId();
- switch(id){
- case 1:
-
- if(srcBitmap != null){
- dstBitmap = getDstImage(null);
- dstImageView.setImageBitmap(dstBitmap);
- }
- break;
- case 2:
-
- if(srcBitmap != null){
- dstBitmap = transferImage();
- dstImageView.setImageBitmap(dstBitmap);
- }
- break;
- case 3:
-
- if(srcBitmap != null){
- dstBitmap = ajustImage();
- dstImageView.setImageBitmap(dstBitmap);
- }
- break;
- case 4:
- if(srcBitmap != null && dstBitmap != null){
- dstBitmap = compositeImages();
- dstImageView.setImageBitmap(dstBitmap);
- }
- break;
- }
- return true;
- }
-
-
-
-
-
-
-
-
-
-
-
-
- private Bitmap getDstImage(Matrix matrix){
-
- Bitmap bmp = null;
-
-
- bmp = Bitmap.createBitmap(srcBitmap.getWidth(), srcBitmap.getHeight(), srcBitmap.getConfig());
-
- Canvas canvas = new Canvas(bmp);
-
- Paint paint = new Paint();
-
-
- if(matrix != null){
-
- canvas.drawBitmap(dstBitmap, matrix, paint);
- }else{
- canvas.drawBitmap(srcBitmap, 0, 0, paint);
- }
-
-
- return bmp;
-
- }
-
-
-
-
-
-
-
-
- private Bitmap getDstImage(Matrix matrix, Paint paint){
-
- Bitmap bmp = null;
-
-
- bmp = Bitmap.createBitmap(srcBitmap.getWidth(), srcBitmap.getHeight(), srcBitmap.getConfig());
-
- Canvas canvas = new Canvas(bmp);
-
-
-
- if(matrix != null){
-
- canvas.drawBitmap(dstBitmap, matrix, paint);
- }else{
- canvas.drawBitmap(srcBitmap, 0, 0, paint);
- }
-
-
- return bmp;
-
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- private Bitmap transferImage(){
- Matrix matrix = new Matrix();
- matrix.setValues(new float[]{
- .5f,0,0,
- 0,1,0,
- 0,0,1
- });
- return this.getDstImage(matrix);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- private Bitmap ajustImage(){
- ColorMatrix cMatrix = new ColorMatrix();
-
-
-
-
-
-
-
-
-
- cMatrix.setScale(2.0f, 2.0f, 2.0f, 2.0f);
- Paint paint = new Paint();
- paint.setColorFilter(new ColorMatrixColorFilter(cMatrix));
- return this.getDstImage(null, paint);
- }
-
-
-
-
-
-
-
-
-
-
- private Bitmap compositeImages(){
-
- Bitmap bmp = null;
-
- bmp = Bitmap.createBitmap(srcBitmap.getWidth(), srcBitmap.getHeight(), srcBitmap.getConfig());
- Paint paint = new Paint();
- Canvas canvas = new Canvas(bmp);
-
- canvas.drawBitmap(srcBitmap, 0, 0, paint);
-
-
-
-
- paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY));
- canvas.drawBitmap(dstBitmap, 0, 0, paint);
-
- return bmp;
- }
- public void onActivityResult(int requestCode, int resultCode, Intent data){
- super.onActivityResult(requestCode, resultCode, data);
-
- Log.v("Result OK Value:", resultCode+"");
- Log.v("RequestCode Value", requestCode+"");
-
- if(resultCode == RESULT_OK){
- p_w_picpathUri = data.getData();
- if(requestCode == FIRST_PIC){
-
- srcBitmap = getSrcImage(p_w_picpathUri);
- srcImageView.setImageBitmap(srcBitmap);
- }else if(requestCode == SECOND_PIC){
-
-
- dstBitmap = getSrcImage(p_w_picpathUri);
- dstImageView.setImageBitmap(dstBitmap);
- }
- }
- }
-
-
-
-
-
- private Bitmap getSrcImage(Uri p_w_picpathUri){
-
- try {
- BitmapFactory.Options ops = new BitmapFactory.Options();
- ops.inJustDecodeBounds = true;
- Bitmap bmp = BitmapFactory.decodeStream(this.getContentResolver().openInputStream(p_w_picpathUri),null,ops);
- int wRatio = (int)Math.ceil(ops.outWidth/(float)MAX_WIDTH);
- int hRatio = (int)Math.ceil(ops.outHeight/(float)MAX_HEIGHT);
-
- if(wRatio > 1 && hRatio > 1){
- if(wRatio > hRatio){
- ops.inSampleSize = wRatio;
- }else{
- ops.inSampleSize = hRatio;
- }
- }
-
- ops.inJustDecodeBounds = false;
- bmp = BitmapFactory.decodeStream(this.getContentResolver().openInputStream(p_w_picpathUri),null,ops);
-
- return bmp;
-
- } catch (FileNotFoundException e) {
-
- e.printStackTrace();
- Log.e(this.getClass().getName(), e.getMessage());
- }
-
- return null;
- }
- }