Android 使用内置的Camera应用程序捕获图像

xiaoxiao2021-02-28  91

Camera Intent Filter过滤器

Camera应用程序在清单文件中指定了以下意图过滤器。

<intent-filter> <action android:name="android.media.action.IMAGE_CAPTURE"/> <category android:name="android.intent.category.DEFAULT"> </intent-filter>

从Camera应用程序返回数据

通过startActivityForResult方法,允许我们访问从Camera应用程序中返回的数据,以位图(Bitmap)形式捕获的图像。 代码参考如下:

Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(i, 0); -------- protected void onActivityResult(int requestCode, int resultCode, Intent intent){ super.onActrivityResult(requestCode, resultCode, intent); if(requestCode == RESULT_OK){ // get the bitmap Bundle extras = intent.getExtras(); Bitmap bmp = (Bitmap) extras.get("data"); // use the bitmap mImageView.setImageBitmap(bmp); } }

注意点: 1.使用MediaStore类中的常量ACTION_IMAGE_PICTURE, 而不要直接使用“android.media.action.IMAGE_CAPTURE”, 使用常量更能适应未来的变化。 2.返回的图像可能较小,可根据具体机器进行调试修改。

捕获大图并指定保存位置

String imageFilePath = Environment.getExternalStorageDirectory().getAbsolutePath()+"/helloBob.jpg"; File imageFile = new File(imageFilePath); Uri imageFileUri = Uri.fromFile(imageFile); ------- Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); i.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageFileUri); startActivityForResult(i, 0);

获取到大图后,需要优化加载Bitmap,不然可能出现大图加载内存溢出的问题,请参考我的另一篇文章: Android Bitmap在不加载图片的前提获取宽高

转载请注明原文地址: https://www.6miu.com/read-26112.html

最新回复(0)