• Camera摄像头


     

    <LinearLayout
        android:id="@+id/btn_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal">
    
        <Button
            android:id="@+id/btn1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="btnClick1"
            android:text="拍照1"/>
    
        <Button
            android:id="@+id/btn2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="btnClick2"
            android:text="拍照2"/>
    </LinearLayout>
    
    <ImageView
        android:id="@+id/iv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/btn_layout"/>

    两种读取图片的方法

    1.在拍照的时候传递一个文件地址,展示图片时即从该地址读取

    2.拍照的时候将图片保存到数据库,展示图片时选取数据库最近的一张

    public class MainActivity extends AppCompatActivity {
    
        private ImageView iv;
        private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd-HH:mm:ss");
        private File file;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            iv = ((ImageView) findViewById(R.id.iv));
        }
    
        public void btnClick1(View view) {
            Intent intent = new Intent();
            //打开系统摄像头
            intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
            //设置所拍照片的保存路径
            file = new File(Environment
                    .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "Image-" + dateFormat.format(new Date()));
            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
            startActivityForResult(intent, 1);
        }
    
        //data中携带的数据是所拍照片的缩略图
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            //如果拍照成功
            if (requestCode == 1 && resultCode == Activity.RESULT_OK) {
                /***************************不添加图片保存路径时调用****************************/
                //提取data中携带的Bitmap数据(如果在启动系统相机的时候已经指定了图片的保存位置,则这里不会返回缩略图)
    //            Bitmap bitmap = (Bitmap) data.getExtras().get("data");
    //            iv.setImageBitmap(bitmap);
    //            Log.d("lenve", "onActivityResult: " + bitmap.getWidth() + ";height:" + bitmap.getHeight());
                /****************************添加图片保存路径后调用这里的方法,使用创建File的方式来构造一个Uri*****************************************/
    //            iv.setImageBitmap(BitmapFactory.decodeFile(file.getAbsolutePath()));
                /******************************使用ContentResolver来获得一个Uri**********************************************************************/
                Cursor cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[]{MediaStore.Images.Media.DATA}
                        , null, null, MediaStore.Images.Media.DATE_ADDED + " desc");
                String imageUrl = null;
                if (cursor.moveToFirst()) {
                    imageUrl = cursor.getString(0);
                    iv.setImageBitmap(BitmapFactory.decodeFile(imageUrl));
                }
                cursor.close();
            }
        }
    
        public void btnClick2(View view) {
            Intent intent = new Intent();
            intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
            ContentValues values = new ContentValues();
            //设置图片名称
            values.put(MediaStore.Images.Media.DISPLAY_NAME, "Image-" + dateFormat.format(new Date()) + ".png");
            values.put(MediaStore.Images.Media.MIME_TYPE, "image/png");
            //往图片数据库中存储数据
            Uri uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
            startActivityForResult(intent, 1);
        }
    }

     

  • 相关阅读:
    html+css 笔记
    JS随手笔记
    JQ几个淡入淡效果
    AngularJS编译阶段应分为两个阶段
    JavaScript 原型链的理解
    js继承的6种方式
    什么是跨域?跨域解决方法
    computed (计算属性) 和 methods (方法) 的区别
    谈谈vue生命周期
    基本类型有哪几种?null 是对象吗?基本数据类型和复杂数据类型存储有什么区别?
  • 原文地址:https://www.cnblogs.com/anni-qianqian/p/5462328.html
Copyright © 2020-2023  润新知