• 热门专题

android.graphics.Camera实现图像的旋转、缩放

作者:CSDN博客博主Sodino  发布日期:2011-09-26 19:01:40
Tag标签:android.graphics.Camera  实现  图像  
  • android.graphics.Camera可以对图像执行一些比较复杂的操作,诸如旋转与绽放。

    本文为练习Camera的使用。实现效果图如下:

     

    Camera的处理结果只是生成一个Matrix,该Matrix用于Bitmap或Canvas绘制Bitmap时才能产生旋转或缩放的效果。

    代码如下:

     

    package lab.sodino.camera;  
      
    import android.app.Activity;  
    import android.graphics.Bitmap;  
    import android.graphics.Camera;  
    import android.graphics.Matrix;  
    import android.graphics.drawable.BitmapDrawable;  
    import android.os.Bundle;  
    import android.util.Log;  
    import android.widget.ImageView;  
    import android.widget.SeekBar;  
    import android.widget.SeekBar.OnSeekBarChangeListener;  
    import android.widget.TextView;  
      
    /** 
     * @author Sodino E-mail:sodinoopen@hotmail.com 
     * @version Time:2011-9-26 下午04:17:49 
     */  
    public class ActCamera extends Activity implements OnSeekBarChangeListener {  
        private Camera camera;  
        // views   
        private SeekBar seekbarXRotate;  
        private SeekBar seekbarYRotate;  
        private SeekBar seekbarZRotate;  
        private TextView txtXRotate;  
        private TextView txtYRotate;  
        private TextView txtZRotate;  
        private SeekBar seekbarXTranslate;  
        private SeekBar seekbarYTranslate;  
        private SeekBar seekbarZTranslate;  
        private TextView txtXTranslate;  
        private TextView txtYTranslate;  
        private TextView txtZTranslate;  
        private ImageView imgResult;  
        // integer params   
        private int rotateX, rotateY, rotateZ;  
        private int translateX, translateY, translateZ;  
      
        /** Called when the activity is first created. */  
        @Override  
        public void onCreate(Bundle savedInstanceState) {  
            super.onCreate(savedInstanceState);  
            setContentView(R.layout.main);  
            // camera   
            camera = new Camera();  
            // initViews   
            // rotate   
            seekbarXRotate = (SeekBar) findViewById(R.id.seekbarXRotate);  
            seekbarXRotate.setOnSeekBarChangeListener(this);  
            seekbarYRotate = (SeekBar) findViewById(R.id.seekbarYRotate);  
            seekbarYRotate.setOnSeekBarChangeListener(this);  
            seekbarZRotate = (SeekBar) findViewById(R.id.seekbarZRotate);  
            seekbarZRotate.setOnSeekBarChangeListener(this);  
            txtXRotate = (TextView) findViewById(R.id.txtXRotate);  
            txtYRotate = (TextView) findViewById(R.id.txtYRotate);  
            txtZRotate = (TextView) findViewById(R.id.txtZRotate);  
            // translate   
            seekbarXTranslate = (SeekBar) findViewById(R.id.seekbarXTranslate);  
            seekbarXTranslate.setOnSeekBarChangeListener(this);  
            seekbarYTranslate = (SeekBar) findViewById(R.id.seekbarYTranslate);  
            seekbarYTranslate.setOnSeekBarChangeListener(this);  
            seekbarZTranslate = (SeekBar) findViewById(R.id.seekbarZTranslate);  
            seekbarZTranslate.setOnSeekBarChangeListener(this);  
            txtXTranslate = (TextView) findViewById(R.id.txtXTranslate);  
            txtYTranslate = (TextView) findViewById(R.id.txtYTranslate);  
            txtZTranslate = (TextView) findViewById(R.id.txtZTranslate);  
            imgResult = (ImageView) findViewById(R.id.imgResult);  
            // refresh   
            refreshImage();  
        }  
      
        private void refreshImage() {  
            // 获取待处理的图像   
            BitmapDrawable tmpBitDra = (BitmapDrawable) getResources().getDrawable(R.drawable.honeycomb);  
            Bitmap tmpBit = tmpBitDra.getBitmap();  
            // 开始处理图像   
            // 1.获取处理矩阵   
            // 记录一下初始状态。save()和restore()可以将图像过渡得柔和一些。   
            // Each save should be balanced with a call to restore().   
            camera.save();  
            Matrix matrix = new Matrix();  
            // rotate   
            camera.rotateX(rotateX);  
            camera.rotateY(rotateY);  
            camera.rotateZ(rotateZ);  
            // translate   
            camera.translate(translateX, translateY, translateZ);  
            camera.getMatrix(matrix);  
            // 恢复到之前的初始状态。   
            camera.restore();  
            // 设置图像处理的中心点   
            matrix.preTranslate(tmpBit.getWidth() >> 1, tmpBit.getHeight() >> 1);  
            // 2.通过矩阵生成新图像(或直接作用于Canvas)   
            Log.d("ANDROID_LAB", "width=" + tmpBit.getWidth() + " height=" + tmpBit.getHeight());  
            Bitmap newBit = null;  
            try {  
                // 经过矩阵转换后的图像宽高有可能不大于0,此时会抛出IllegalArgumentException   
                newBit = Bitmap.createBitmap(tmpBit, 0, 0, tmpBit.getWidth(), tmpBit.getHeight(), matrix, true);  
            } catch (IllegalArgumentException iae) {  
                iae.printStackTrace();  
            }  
            if (newBit != null) {  
                imgResult.setImageBitmap(newBit);  
            }  
        }  
      
        @Override  
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {  
            if (seekBar == seekbarXRotate) {  
                txtXRotate.setText(progress + "゜");  
                rotateX = progress;  
            } else if (seekBar == seekbarYRotate) {  
                txtYRotate.setText(progress + "゜");  
                rotateY = progress;  
            } else if (seekBar == seekbarZRotate) {  
                txtZRotate.setText(progress + "゜");  
                rotateZ = progress;  
            } else if (seekBar == seekbarXTranslate) {  
                translateX = progress - 100;  
                txtXTranslate.setText(String.valueOf(translateX));  
            } else if (seekBar == seekbarYTranslate) {  
                translateY = progress - 100;  
                txtYTranslate.setText(String.valueOf(translateY));  
            } else if (seekBar == seekbarZTranslate) {  
                translateZ = progress - 100;  
                txtZTranslate.setText(String.valueOf(translateZ));  
            }  
            refreshImage();  
        }  
      
        @Override  
        public void onStartTrackingTouch(SeekBar seekBar) {  
      
        }  
      
        @Override  
        public void onStopTrackingTouch(SeekBar seekBar) {  
      
        }  
    }  
    

    本文内容归CSDN博客博主Sodino 所有
     

About IT165 - 广告服务 - 隐私声明 - 版权申明 - 免责条款 - 网站地图 - 网友投稿 - 联系方式
本站内容来自于互联网,仅供用于网络技术学习,学习中请遵循相关法律法规