Android_SystemCamera

来源:互联网 发布:网络命令的使用 编辑:程序博客网 时间:2024/06/07 00:48
public class CameraImageActivity extends AppCompatActivity {    private ImageView mIv;    private String imgPath;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_camera_image);        mIv = (ImageView) findViewById(R.id.my_iv);        imgPath = getExternalCacheDir().getAbsolutePath()+File.separator+"abc.png";        Log.d("sxl", "onCreate: "+imgPath);    }    public void camera(View view)    {        switch (view.getId())        {            case R.id.camera_but_01:                //跳转到拍照界面                Intent intent = new Intent();                intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);                startActivityForResult(intent,1);                break;            case R.id.camera_but_02:                //跳转到拍照界面                Intent intent1 = new Intent();                intent1.setAction(MediaStore.ACTION_IMAGE_CAPTURE);                //向意图当中添加内容,包括文件想要存储的路径                //存储到外面的, Uri的地址                //向意图对象中, 传入指定的路径                Uri uri = Uri.fromFile(new File(imgPath));                Log.d("sxl",uri.toString());                intent1.putExtra(MediaStore.EXTRA_OUTPUT, uri);                startActivityForResult(intent1,2);                break;        }    }    @Override    protected void onActivityResult(int requestCode, int resultCode, Intent data) {        super.onActivityResult(requestCode, resultCode, data);        if (resultCode == RESULT_OK)        {            if (requestCode==1)            {                //拍照结束                //获取传递回来的数据, 照片                Bundle bundle = data.getExtras();                Bitmap bitmap = (Bitmap) bundle.get("data");                if (bitmap!=null) {                    mIv.setImageBitmap(bitmap);                }            } else if(requestCode==2) {                //制作缩略图                //图片的参数                BitmapFactory.Options options = new BitmapFactory.Options();                //true代表  不做真正的转图像(不做解码)  只是获取  如果图像太大直接解码会引起内存溢出                options.inJustDecodeBounds = true;                //将这个路径的文件转为图片 只是获取了图片的属性  并不是真正的转                Bitmap bm = BitmapFactory.decodeFile(imgPath,options);                //获取图片的原始宽和高                int oldWidth = options.outWidth;                int oldHeight =options.outHeight;                //将图片的宽和高都缩小200倍                int sampleWidth = oldWidth/200;                int sampleHeight = oldHeight/200;                //获取宽和高的最大值                int size = Math.max(sampleWidth,sampleHeight);                //设置缩放比例                options.inSampleSize = size;                //false代表 做真正的转图像(也就是做解码)                options.inJustDecodeBounds = false;                //将这个路径的文件转为图片 是真正的转                Bitmap bitmap = BitmapFactory.decodeFile(imgPath,options);                //为ImagView设置资源                mIv.setImageBitmap(bitmap);            }        }    }}

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"              android:id="@+id/activity_camera_image"              android:layout_width="match_parent"              android:layout_height="match_parent">    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/camera_but_01"        android:text="点击打开摄像头"        android:onClick="camera"/>    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/camera_but_02"        android:text="拍照, 并且存入指定的路径中"        android:layout_below="@id/camera_but_01"        android:onClick="camera"/>    <ImageView        android:layout_width="300dp"        android:layout_height="300dp"        android:id="@+id/my_iv"        android:src="@mipmap/ic_launcher"        android:layout_below="@id/camera_but_02"        android:layout_centerHorizontal="true"/></RelativeLayout>

原创粉丝点击