使用内置摄像头并优化显示结果大图片的方法

简介: 使用内置摄像头并优化显示结果大图片的方法

1.将BitmapFactory.Options.inJustDecodeBounds变量设置为true,这表示通知BitmapFactory类只需返回该类图像的范围,而不用解码图像本身。使用此方法,BitmapFactory.Options.outHeight和BitmapFactory.Options.outWidth变量将会被赋值。


2.通过给内置的Camera应用程序传递一个附加值(该附加值在MediaStore类中指定,MediaStore.EXTRA_OUTPUT),我们能以Uri的形式指定Camera应用保存捕获图像的位置。以下的示例就将图像保存在SD卡中。注意:别忘了在AndroidManifest.xml文件中添加以下语句:

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

3.BimapFactory.Options.inSampleSize表示加载时结果图像所占的比例。

public class SizedImageCamera extends Activity
{
  final static int CAMERA_RESULT = 0;
  ImageView imv;
  String imageFilePath;
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data)
  {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode==RESULT_OK)
    {
      imv=(ImageView)findViewById(R.id.ImageView);
      Display currentDisplay=getWindowManager().getDefaultDisplay();
      int dw=currentDisplay.getWidth();
      int dh=currentDisplay.getHeight();
//      加载图像的尺寸而不是图像本身
      BitmapFactory.Options bmpFactoryOptions=new BitmapFactory.Options();
      bmpFactoryOptions.inJustDecodeBounds=true;
      Bitmap bmp=BitmapFactory.decodeFile(imageFilePath, bmpFactoryOptions);
      int heightRatio=(int)Math.ceil(bmpFactoryOptions.outHeight/(float)dh);
      int widthRatio=(int)Math.ceil(bmpFactoryOptions.outWidth/(float)dw);
      Log.v("HEIGHT RATIO",""+heightRatio);
      Log.v("WIDTH RATIO",""+widthRatio);
//      如果两个比率都大于1,那么图像的一条边将大于屏幕
      if(heightRatio>1&&widthRatio>1)
      {
        if(heightRatio>widthRatio)
        {
//          如果高度比率更大,则根据它缩放
          bmpFactoryOptions.inSampleSize=heightRatio;
        }
        else
        {
//          若宽度比率更大,则根据它缩放
          bmpFactoryOptions.inSampleSize=widthRatio;
        }
      }
//      对它进行真正的解码
      bmpFactoryOptions.inJustDecodeBounds=false;
      bmp=BitmapFactory.decodeFile(imageFilePath, bmpFactoryOptions);
      imv.setImageBitmap(bmp);
    }
  }
  @Override
  protected void onCreate(Bundle savedInstanceState)
  {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    imageFilePath = Environment.getExternalStorageDirectory()
        .getAbsolutePath() + "mypicture.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, CAMERA_RESULT);
  }
}

4.给图像添加元数据ContentValues

(1)预填充

ContentValues contentValues=new ContentValues(3);
contentValues.put(Media.DISPLAY_NAME,"title");
contentValues.put(Media.DESCRIPTION,"description");
contentValues.put(Media.MIME_TYPE,"image/jpeg");
Uri imageFileUri=getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, contentValues);

获得的Uri的值为content://media/external/images/media/16


(2)后期填充

ContentValues contentValues=new ContentValues(3);
contentValues.put(Media.DISPLAY_NAME,"title");
contentValues.put(Media.DESCRIPTION,"description");
contentValues.put(Media.MIME_TYPE,"image/jpeg");
getContentResolver().update(imageFileUri, contentValues,null,null);

5.获取保存的图像

Bitmap bmp=BitmapFactory.decodeStream(getContentResolver().openInputStream(imageFileUri),null,bmpFactoryOptions);

6.View.setVisibility

setVisibility(View.GONE):将用户的元素都设置为不可见,且不占用布局空间

setVisibility(View.INVISIBLE):将隐藏元素,但是占用布局空间

目录
相关文章
|
Ubuntu 定位技术 API
python 通过图片(原图)精确获取图片拍摄的位置,时间,设备等信息
python 通过图片(原图)精确获取图片拍摄的位置,时间,设备等信息
python 通过图片(原图)精确获取图片拍摄的位置,时间,设备等信息
|
1月前
|
开发工具 数据安全/隐私保护 Android开发
视觉智能平台常见问题之图片解析出的水印图判断是自己添加的水印图如何解决
视觉智能平台是利用机器学习和图像处理技术,提供图像识别、视频分析等智能视觉服务的平台;本合集针对该平台在使用中遇到的常见问题进行了收集和解答,以帮助开发者和企业用户在整合和部署视觉智能解决方案时,能够更快地定位问题并找到有效的解决策略。
19 1
|
8月前
|
编解码 前端开发 JavaScript
响应式图像优化:如何根据用户设备和网络条件优化网页中的图像,以提高用户体验和加载速度。
响应式图像优化:如何根据用户设备和网络条件优化网页中的图像,以提高用户体验和加载速度。
|
Web App开发 移动开发 前端开发
移动端图片操作(二)——预览、旋转、合成
在上一节中已经提到了预览,预览可以通过data: URL格式或URL对象。
移动端图片操作(二)——预览、旋转、合成
|
9月前
|
编解码
漏刻有时拼接屏测试分辨率的测试页面
漏刻有时拼接屏测试分辨率的测试页面
41 0
|
算法 小程序 Java
图像中二维码的检测和定位
图像中二维码的检测和定位
1004 0
图像中二维码的检测和定位
|
移动开发 JavaScript
如何在h5页面中调用摄像头来完成拍照之类的操作
如何在h5页面中调用摄像头来完成拍照之类的操作
620 0
如何在h5页面中调用摄像头来完成拍照之类的操作
|
传感器 算法 API
Flir Blackfly S 工业相机:自动曝光配置及代码
自动曝光通过调整增益和曝光时间来优化图像的亮度。 自动曝光在相机中进行的话则减少了CPU后期的处理运算。
Flir Blackfly S 工业相机:自动曝光配置及代码