使用内置摄像头并优化显示结果大图片的方法
2013-03-23 15:37
351 查看
1.将BitmapFactory.Options.inJustDecodeBounds变量设置为true,这表示通知BitmapFactory类只需返回该类图像的范围,而不用解码图像本身。使用此方法,BitmapFactory.Options.outHeight和BitmapFactory.Options.outWidth变量将会被赋值。
2.通过给内置的Camera应用程序传递一个附加值(该附加值在MediaStore类中指定,MediaStore.EXTRA_OUTPUT),我们能以Uri的形式指定Camera应用保存捕获图像的位置。以下的示例就将图像保存在SD卡中。注意:别忘了在AndroidManifest.xml文件中添加以下语句:
4.给图像添加元数据ContentValues
(1)预填充
(2)后期填充
5.获取保存的图像
6.View.setVisibility
setVisibility(View.GONE):将用户的元素都设置为不可见,且不占用布局空间
setVisibility(View.INVISIBLE):将隐藏元素,但是占用布局空间
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):将隐藏元素,但是占用布局空间
相关文章推荐
- (原创)使用AsyncTask(带修改线程池方式)+自定义ImageLoader+LRU算法对图片三级缓存及其显示优化(只有在ListView滑动停止的时候才去网络请求获取图片数据)
- (转载) UITableView 系列一 :基本使用方法 (显示,删除,添加图片,添加样式等
- Html中使用自定义图片来实现checkbox显示的方法
- 【Vegas原创】访问owa,图片和js脚本不能正常显示和使用的解决方法
- C++中使用CAtlHttpClient显示网络图片的方法,支持png,bmp,gif图片格式
- 新ALT使用方法,可显示图片、文字有渐隐效果
- JLabel显示图片,及其它使用方法。。。实现九九乘法表
- Jquery中使用show()与hide()方法动画显示和隐藏图片
- Swift中图片资源使用流程的优化方法详解
- ViewPager显示广告图片超简易使用方法
- pyqt5 使用cv2 显示图片,摄像头
- Android 网络本地显示图片Picasso优化封装使用
- 优化ImageView,在使用ListView或GridView的时候,每次更新图片的时候,在显示新的图片和旧的图片一样大小的时候,不需要去重新计算大小
- MFC下使用CvvImage在子对话框(非主对话框)中无法显示图片的解决方法
- JLabel显示图片,及其它使用方法。。。实现九九乘法表
- jQuery中fadeIn、fadeOut、fadeTo的使用方法(图片显示与隐藏)
- 使用Markdown编辑器插入图片只显示连接的解决方法
- VS2008中使用JSONCPP方法修改版本能运行显示结果
- WPF使用Image控件图片无法显示的解决方法
- jQuery中fadeIn、fadeOut、fadeTo的使用方法(图片显示与隐藏)