Android -- 圖片處理, 畫畫板,縮放,旋轉,平移,鏡面,倒影,圖片合成,顏色處理

原文:http://www.cnblogs.com/xj626852095/p/3645055.htmlhtml

1. 畫畫板 示例代碼android

public class MainActivity extends Activity {
    private ImageView iv;
    private Bitmap baseBitmap;
    private Canvas canvas;
    private Paint paint;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iv = (ImageView) findViewById(R.id.iv);
        paint = new Paint();
        paint.setStrokeWidth(5);
        paint.setColor(Color.GREEN);
        
        // 建立一個能夠被修改的bitmap
        baseBitmap = Bitmap.createBitmap(320, 359,
                Bitmap.Config.ARGB_8888);
        System.out.println("圖寬度:"+iv.getWidth());
        System.out.println("圖寬高:"+iv.getHeight());
        canvas = new Canvas(baseBitmap);
        canvas.drawColor(Color.WHITE);

        // 知道用戶手指在屏幕上移動的軌跡
        iv.setOnTouchListener(new OnTouchListener() {
            //定義手指開始位置的座標
            int startX;
            int startY;
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN: // 手指第一次接觸屏幕
                    System.out.println("手指按下");
    
                    startX = (int) event.getX();
                    startY = (int) event.getY();
                    break;
                case MotionEvent.ACTION_MOVE: // 手指在屏幕上滑動
                    System.out.println("手指移動");
                    int newX  = (int) event.getX();
                    int newY = (int) event.getY();
                    
                    canvas.drawLine(startX,startY, newX, newY, paint);
                    //從新更新畫筆的開始位置.
                    startX = (int) event.getX();
                    startY = (int) event.getY();
                    iv.setImageBitmap(baseBitmap);
                    break;
                case MotionEvent.ACTION_UP:// 手指離開屏幕

                    break;
                }

                return true;
            }
        });

    }

    public void save(View view) {
        try {
            File file = new File(Environment.getExternalStorageDirectory(),System.currentTimeMillis()+".jpg");
            FileOutputStream stream = new FileOutputStream(file);
            baseBitmap.compress(CompressFormat.JPEG, 100, stream);
            stream.close();
            Toast.makeText(this, "保存圖片成功", 1).show();
            
            //模擬一個消息 通知系統sd卡被從新掛載了.
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_MEDIA_MOUNTED);
            intent.setData(Uri.fromFile(Environment.getExternalStorageDirectory()));
            sendBroadcast(intent);
            
            
        } catch (Exception e) {
            Toast.makeText(this, "保存圖片失敗", 1).show();
            e.printStackTrace();
        }
        
        
        
    }

}

2. 圖片縮放canvas

public class MainActivity extends Activity {
    private ImageView iv_src;
    private ImageView iv_dest;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iv_src = (ImageView) findViewById(R.id.iv_src);
        iv_src.setImageBitmap(BitmapFactory.decodeFile("/sdcard/tom.png"));
        iv_dest = (ImageView) findViewById(R.id.iv_dest);

    }

    public void click(View view) {
        Bitmap bitmap = BitmapFactory.decodeFile("/sdcard/tom.png");
        Bitmap baseBitmap = Bitmap.createBitmap(bitmap.getWidth()*2,
                bitmap.getHeight()/2, bitmap.getConfig());
        Canvas canvas = new Canvas(baseBitmap);
        Matrix matrix = new Matrix();
    /*    matrix.setValues(new float[] {
                2, 0, 0,
                0, 0.5f, 0,
                0, 0, 1
            });*/
        matrix.setScale(2.0f, 0.5f);
        canvas.drawBitmap(bitmap, matrix, new Paint());
        iv_dest.setImageBitmap(baseBitmap);
    }
}

3. 圖片旋轉ide

Matrix matrix = new Matrix();

matrix.setRotate(15);

canvas.drawBitmap(bmp, matrix, paint);

消除鋸齒

paint.setAntiAlias(true);  

指定圓心的旋轉

matrix.setRotate(15,bmp.getWidth()/2,bmp.getHeight()/2);

4. 圖片平移post

matrix.setTranslate(1.5f,-10);this

5. 鏡面效果code

matrix.setScale(-1, 1);orm

matrix.postTranslate(bmp.getWidth(),0);htm

6. 倒影blog

matrix.setScale(1, -1);

matrix.postTranslate(0, bmp.getHeight());

7. 圖片合成

public class MainActivity extends Activity {
    private ImageView iv;    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iv = (ImageView) findViewById(R.id.iv);
        
        //第一張圖
          Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.a1);
        //第二張圖
          Bitmap addbitMap = BitmapFactory.decodeResource(getResources(), R.drawable.a3);
        
        //可修改的位圖 及 畫板
          Bitmap alterBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
        Canvas canvas = new Canvas(alterBitmap);
        
        Paint point = new Paint();
        point.setColor(Color.BLACK);
        point.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.SRC_OVER));//合成模式
        
        //將第一張圖和第二張圖畫到畫板的可修改位圖上
          canvas.drawBitmap(bitmap, new Matrix(), point);                
        canvas.drawBitmap(addbitMap, new Matrix(), point);
        
        iv.setImageBitmap(alterBitmap);                
    }
}

 8. 顏色處理,RGB顏色, 亮度, 飽和度

亮度處理

ColorMatrix cm = new ColorMatrix();
float contrast = 2;
cm.set(new float[] {
    contrast, 0, 0, 0, 0,
    0, contrast, 0, 0, 0,
    0, 0, contrast, 0, 0,
    0, 0, 0, 1, 0 });
paint.setColorFilter(new ColorMatrixColorFilter(cm));

RGB 及 飽和度 

public class MainActivity extends Activity {

    private ImageView iv1;
    private SeekBar sb1;
    private SeekBar sb2;
    private SeekBar sb3;
    private SeekBar sb4;
            
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main2);
        iv1 = (ImageView) findViewById(R.id.iv_1);
        sb1 = (SeekBar) findViewById(R.id.seekBar1);
        sb2 = (SeekBar) findViewById(R.id.seekBar2);
        sb3 = (SeekBar) findViewById(R.id.seekBar3);
        sb4 = (SeekBar) findViewById(R.id.seekBar4);
        sb1.setMax(255);
        sb2.setMax(255);
        sb3.setMax(255);
        sb4.setMax(255);
        
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.top);
        final Bitmap baseBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
        
        final Canvas canvas = new Canvas(baseBitmap);
        final Matrix matrix = new Matrix();
        final Paint paint = new Paint();
        
        canvas.drawBitmap(bitmap, matrix, paint);
        iv1.setImageBitmap(baseBitmap);
        
        //調節R 紅色
        sb1.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {            
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                
            }            
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                
            }            
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                    boolean fromUser) {
                int ret = seekBar.getProgress();
                if(ret!=0)
                {
                    ColorMatrix cm = new ColorMatrix();
                    cm.set(new float[]{
                        ret/128.0f,0,0,0,0,    
                        0,1,0,0,0,    
                        0,0,1,0,0,    
                        0,0,0,1,0,    
                    });
                    paint.setColorFilter(new ColorMatrixColorFilter(cm));
                    canvas.drawBitmap(baseBitmap, matrix, paint);
                    iv1.setImageBitmap(baseBitmap);
                }
            }
        });
        
        //調節G 綠色
        sb2.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {            
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                
            }            
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                
            }            
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                    boolean fromUser) {
                int ret = seekBar.getProgress();
                if(ret!=0)
                {
                    ColorMatrix cm = new ColorMatrix();
                    cm.set(new float[]{
                        1,0,0,0,0,    
                        0,ret/128.0f,0,0,0,    
                        0,0,1,0,0,    
                        0,0,0,1,0,    
                    });
                    paint.setColorFilter(new ColorMatrixColorFilter(cm));
                    canvas.drawBitmap(baseBitmap, matrix, paint);
                    iv1.setImageBitmap(baseBitmap);
                }
            }
        });
        
        //調節B 藍色
        sb3.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {            
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                
            }            
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                
            }            
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                    boolean fromUser) {
                int ret = seekBar.getProgress();
                if(ret!=0)
                {
                    ColorMatrix cm = new ColorMatrix();
                    cm.set(new float[]{
                        1,0,0,0,0,    
                        0,1,0,0,0,    
                        0,0,ret/128.0f,0,0,    
                        0,0,0,1,0,    
                    });
                    paint.setColorFilter(new ColorMatrixColorFilter(cm));
                    canvas.drawBitmap(baseBitmap, matrix, paint);
                    iv1.setImageBitmap(baseBitmap);
                }
            }
        });
        
        //調節 飽和度
        sb4.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {            
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {                
            }            
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {                
            }            
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                    boolean fromUser) {
                int ret = seekBar.getProgress();
                if(ret!=0)
                {
                    ColorMatrix cm = new ColorMatrix();
                    cm.setSaturation(ret/128.0f);
                    paint.setColorFilter(new ColorMatrixColorFilter(cm));
                    canvas.drawBitmap(baseBitmap, matrix, paint);
                    iv1.setImageBitmap(baseBitmap);
                }
            }
        });
        
    }

}
相關文章
相關標籤/搜索