Android调用系统摄像头拍照并且调整图像尺寸
直接上代码,1.在功能调用处
findViewById(R.id.camera).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(intent, 1); } });
2、捕抓返回数据
protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK) { // 拍照保存完成后 if(requestCode == 1) { String imgPath = getAbsoluteImagePath(data.getData()); myDeputyView.clear(); myDeputyView.insertImg(getSmallBitmap(imgPath)); } } }
3、图片处理
/** * 根据路径获得图片并压缩,返回旋转90度的bitmap用于显示 * */ public static Bitmap getSmallBitmap(String filePath) { final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(filePath, options); options.inSampleSize = calculateInSampleSize(options, 750, 200); options.inJustDecodeBounds = false; int scaleHeight=576; int scaleWidth=768; Matrix matrix = new Matrix(); matrix.reset(); matrix.setRotate(90); Bitmap myBitmap = BitmapFactory.decodeFile(filePath, options); float mWidth = (float)scaleWidth / (float)myBitmap.getWidth(); // target缩放到的宽度 src原始宽度,这样算出来是个比率 float mHeight = (float)scaleHeight / (float)myBitmap.getHeight(); matrix.postScale(mWidth, mHeight); System.out.println(myBitmap.getWidth()+"====myBitmap.getWidth()="+myBitmap.getHeight()); myBitmap = Bitmap.createBitmap(myBitmap, 0, 0, myBitmap.getWidth(), myBitmap.getHeight(),matrix, true); return myBitmap; }
分享到: | |
好久不见了,博客还是这样灯火辉煌嘛!
一般,服务器不好访问的人越来越少了