在Android中实现截图功能

jopen 10年前

在实际应用中,经常需要对图片进行处理,包括压缩、截图等等,其实android系统提供了一个可以截图的activity,我们只需调用它就行了,下面示例完成一个通过相机照相或相册选取图片后对其截图的功能,其实知道图片的路劲都可以调用截图activity,代码如下:

测试activity:

</div>
import java.io.File;        import android.app.Activity;    import android.content.Intent;    import android.graphics.Bitmap;    import android.graphics.drawable.BitmapDrawable;    import android.net.Uri;    import android.os.Bundle;    import android.os.Environment;    import android.provider.MediaStore;    import android.provider.MediaStore.Images.Media;    import android.view.View;    import android.view.View.OnClickListener;    import android.widget.Button;    import android.widget.ImageView;    import android.widget.Toast;        public class TestClipImageActivity extends Activity implements OnClickListener {        private Button gallaryBtn;        private Button cameraBtn;        private ImageView showImage;        private File file;            @Override        protected void onCreate(Bundle savedInstanceState) {            super.onCreate(savedInstanceState);            setContentView(R.layout.main);            gallaryBtn = (Button) findViewById(R.id.main_btn_gallery);            cameraBtn = (Button) findViewById(R.id.main_btn_camera);            gallaryBtn.setOnClickListener(this);            cameraBtn.setOnClickListener(this);            showImage = (ImageView) findViewById(R.id.main_image);        }            @Override        public void onClick(View v) {            if (v == gallaryBtn) {                // 跳转至相册界面                Intent intent = new Intent(Intent.ACTION_PICK,                        Media.EXTERNAL_CONTENT_URI);                startActivityForResult(intent, 0);            }            if (v == cameraBtn) {                if (!Environment.MEDIA_MOUNTED.equals(Environment                        .getExternalStorageState())) {                    Toast.makeText(this, "请插入SD卡", Toast.LENGTH_SHORT).show();                    return;                }                // 跳转至相机界面                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);                file = new File(Environment.getExternalStorageDirectory(),                        getPhotoName());                // 指定相机拍照后相片的存储位置                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));                startActivityForResult(intent, 1);            }        }            @Override        protected void onActivityResult(int requestCode, int resultCode, Intent data) {            super.onActivityResult(requestCode, resultCode, data);            // 系统相册返回            if (requestCode == 0 && resultCode == Activity.RESULT_OK                    && data != null) {                startPhotoZoom(data.getData(), 150);// 截图            }            // 系统相机返回            if (requestCode == 1 && resultCode == Activity.RESULT_OK) {                startPhotoZoom(Uri.fromFile(file), 150);// 截图            }            // 截图后返回            if (requestCode == 2 && data != null) {                Bundle bundle = data.getExtras();                if (bundle != null) {                    Bitmap bitmap = bundle.getParcelable("data");                    showImage.setBackgroundDrawable(new BitmapDrawable(bitmap));                }            }        }            /**        * 跳转至系统截图界面进行截图        *         * @param data        * @param size        */        private void startPhotoZoom(Uri data, int size) {            Intent intent = new Intent("com.android.camera.action.CROP");            intent.setDataAndType(data, "image/*");            // crop为true时表示显示的view可以剪裁            intent.putExtra("crop", "true");            // aspectX aspectY 是宽高的比例            intent.putExtra("aspectX", 1);            intent.putExtra("aspectY", 1);            // outputX,outputY 是剪裁图片的宽高            intent.putExtra("outputX", size);            intent.putExtra("outputY", size);            intent.putExtra("return-data", true);            startActivityForResult(intent, 2);        }            /**         * 产生照片名称         *          * @return         */        private String getPhotoName() {            String name = System.currentTimeMillis() + ".jpg";            return name;        }    }  
</div> 布局文件:
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:orientation="vertical" >                    <LinearLayout                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:gravity="center_horizontal"                android:orientation="horizontal" >                        <Button                    android:id="@+id/main_btn_gallery"                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:text="相册" />                        <Button                    android:id="@+id/main_btn_camera"                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:text="相机" />            </LinearLayout>                    <ImageView                android:id="@+id/main_image"                android:layout_width="150dp"                android:layout_height="150dp"                android:layout_gravity="center_horizontal"                android:layout_marginTop="30dp" />                </LinearLayout>  
</div> </div> 加上权限:

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>        <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
</div>