天天看点

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

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):将隐藏元素,但是占用布局空间