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








上一篇:vue-element 项目开发双语网站


下一篇:SqlServer的sa账号被锁定