zoukankan      html  css  js  c++  java
  • 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);
        }
    }

     

  • 相关阅读:
    通过远程linux shell传输文件
    Windows下 postgresql安装报错
    python安装tesserocr错误解决办法
    django makemigrations
    Windows下eric安装的几个小问题
    _WinMain结构
    python学习第七天——输入输出、file
    python学习第六天——模块的概念(重要)
    python学习第五天——函数(重点)
    python学习第四天——控制流
  • 原文地址:https://www.cnblogs.com/anni-qianqian/p/5462328.html
Copyright © 2011-2022 走看看