一.xml页面代码
<ImageView android:id="@+id/picture" android:layout_width="250dp" android:layout_height="250dp" android:layout_gravity="center_horizontal"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="15dp" android:orientation="horizontal"> <Button android:id="@+id/take_photo" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/bg_button" android:textColor="#ffffff" android:textSize="20dp" android:text="拍照"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="15dp" android:orientation="horizontal"> <Button android:id="@+id/getphoto" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/bg_button" android:textColor="#ffffff" android:textSize="20dp" android:text="相册"/> </LinearLayout>
二.acticity后台控制代码
private static final int TAKE_PHOTO=1; //图片参数 private static final int REQUEST_CODE_ALBUM = 2; // 本地相册 private ImageView picture; //图片控件 private Uri imageUrl; //图片url private String PicBase64=""; //Base64 private static final int REQUEST_CODE_SCAN = 111; //扫描参数 private static final String IMAGE_UNSPECIFIED = "image/*";
//按钮点击触发拍照 private void initBtnClick(){ Button takephoto = (Button)findViewById(R.id.take_photo); takephoto.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //创建File对象,用于存储拍照后的图片 File outputImage=