Android照相机拍照Camera




	Camera、
	1.权限设置
	2.intent、startActivityForResult启动照相
	3.onActivityResult 接收照相的返回值
		3.1、数据图片保dsdcard
		3.2、读取sdcard图片
		3.3、图片加载到imageView上
	4.点击图片;popupwindow或者新页面放大展示


	/**
	 *  1.启动系统的相机拍照
	 */
	private void initPhoto() {
		// 隐式启动 系统相机
		Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		startActivityForResult(intent, 1001);//注意重写:onActivityResult
	}

	@Override//2.
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == 1001) {
			if (null != data && data != null && data.getExtras() != null) {
				try {
					String path = saveImg(data);
					Bitmap bitins = getSdcardImg(path);
					ivLogo.setImageBitmap(bitins);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	/**
	 * 3.1
	 * @方法用于把图片存到本地
	 * @param data  拍照intent返回到图片数据集
	 * @return	返回图片存储本地后的 、详细图片地址
	 * @throws FileNotFoundException  io操作的异常
	 */
	private String saveImg(Intent data) throws FileNotFoundException {
		Bitmap bit = (Bitmap) data.getExtras().get("data");// 得到照相拍照的图片
		String fileName = System.currentTimeMillis() + "";
		// 保存图片 路径 名称
		String path = Environment.getExternalStorageDirectory() + "/" + fileName + ".jpg";
		// 图片路径存入数据库,用于不同用户登录使用
		// 输出流 保存到本地sdcard
		FileOutputStream fos = new FileOutputStream(path);
		// 1.格式 2.质量 3. 输出流保存位置
		boolean b = bit.compress(CompressFormat.JPEG, 100, fos);
		if (b) {
			Toast.makeText(this, " img save successful", Toast.LENGTH_LONG).show();
		} else {
			Toast.makeText(this, " img save false ", Toast.LENGTH_LONG).show();
		}
		return path;
	}

	/**
	 * 3.2
	 * @param path  本地图片地址
	 * @return  返回得到的 Bitmap 图片对象
	 * @throws FileNotFoundException  io读取异常
	 */
	private Bitmap getSdcardImg(String path) throws FileNotFoundException{
		FileInputStream fis = new FileInputStream(path);
		Bitmap bitins = BitmapFactory.decodeStream(fis);
		return bitins;
	}

	//给定图片宽高,返回压缩后的图片
 public static Bitmap zoomBitmap(Bitmap bitmap, int width, int height) {  
        int w = bitmap.getWidth();  
        int h = bitmap.getHeight();  
        Matrix matrix = new Matrix();  
        float scaleWidth = ((float) width / w);  
        float scaleHeight = ((float) height / h);  
        matrix.postScale(scaleWidth, scaleHeight); // 不改变原来图像大小  
        Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);  
        return newbmp;  
    }  


//Activity Send Bitmap To Fragment
    	FragmentManager fm = getFragmentManager();
		FragmentTransaction tran = fm.beginTransaction();
		ImageFragment imgFragment = new ImageFragment();
		Bundle b = new Bundle();
		b.putParcelable("bit", bitins);
		imgFragment.setArguments(b);
		tran.replace(R.id.rl_layout, imgFragment);
		tran.commit();

@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		Bitmap b = (Bitmap) getArguments().get("bit");
		ImageView iv = new ImageView(mContext);
		Log.e(TAG, "====onCreateView========");
		iv.setImageBitmap(b);
		iv.setLayoutParams( new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		return  iv;
	}



	照相机拍照需要的权限:
<!--写sd卡权限-->
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
 <!--读sd卡的权限 -->
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<!--操作摄像头的权限 -->
   <uses-permission android:name="android.permission.CAMERA" />


项目demo下载地址:

http://download.csdn.net/detail/flyingsir_zw/9693403








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值