Android中拍照Camera的简单实用
来源:互联网 发布:mac口红哪个国家买便宜 编辑:程序博客网 时间:2024/04/26 19:53
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:android_custom="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="btn" > </Button> <Button android:id="@+id/btn2" android:layout_below="@+id/btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="btn2" > </Button> <ImageView android:id="@+id/img" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/btn2" > </ImageView></RelativeLayout>
public class MainActivity extends Activity implements OnClickListener {private static final int REQUEST_CODE_IMAGE_CATURE = 0x11;private static final int REQUEST_CODE_IMAGE_CATURE_SAVE = 0x12;ImageView img;String imgPath;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.paomadeng);img = (ImageView) findViewById(R.id.img);Button btn = (Button) findViewById(R.id.btn);Button btn2 = (Button) findViewById(R.id.btn2);btn.setOnClickListener(this);btn2.setOnClickListener(this);}@Overridepublic void onClick(View v) {if (v.getId() == R.id.btn) {takePicture();} else if (v.getId() == R.id.btn2) {savePicture();}}public void takePicture() {Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);startActivityForResult(intent, REQUEST_CODE_IMAGE_CATURE );}public void savePicture() {// File.pathSeparator换成这个会出现问题imgPath = Environment.getExternalStorageDirectory() + File.separator + "tmp.png";File file = new File(imgPath);if (file.exists()) {file.delete();}try {file.createNewFile();} catch (IOException e) {e.printStackTrace();}Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);Uri imgUri = Uri.fromFile(file);intent.putExtra(MediaStore.EXTRA_OUTPUT, imgUri);startActivityForResult(intent, REQUEST_CODE_IMAGE_CATURE_SAVE );}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {super.onActivityResult(requestCode, resultCode, data);if (resultCode != Activity.RESULT_OK) {return;}if (requestCode == REQUEST_CODE_IMAGE_CATURE) {Bundle bundle = data.getExtras();/** * 获取拍照的数据,注意此时还没有生成相关的图片文件 * 还有就是这里获取的bitmap,其实仅仅是缩略图,并不是原始的数据。 */Bitmap bitmap = bundle.getParcelable("data");img.setImageBitmap(bitmap);} else if (requestCode == REQUEST_CODE_IMAGE_CATURE_SAVE) {try {FileInputStream fis = new FileInputStream(new File(imgPath));// 从文件输入流中获取bitmap对象Bitmap bitmap = BitmapFactory.decodeStream(fis);img.setImageBitmap(bitmap);} catch (FileNotFoundException e) {e.printStackTrace();}}}}
注意:需要加入相关的权限。
<uses-permission android:name="android.permission.CAMERA"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
0 0
- Android中拍照Camera的简单实用
- Andorid Camera的简单实用
- android中 camera 拍照应用详解
- android Camera 拍照的两个问题
- android Camera 拍照的两个问题
- Android的Camera用SurfaceView拍照
- 使用Android 5.0的Camera v2拍照
- 简单实用的手机拍照技巧
- android camera 后台拍照
- Android Camera进行拍照
- Android之Camera拍照
- android Camera拍照
- Android Camera进行拍照
- Android+Camera+拍照
- Android Camera拍照实现
- Android Camera 无法拍照
- Android Camera拍照
- Android之Camera拍照
- 被神化的海量数据处理和高并发处理
- bzoj2561 最小割
- HDU 1678 Shopaholic(优先队列 + 排序 + 英文-我想哭)
- 设计模式 速查
- 浙商大会观后感!
- Android中拍照Camera的简单实用
- Mac Android Studio常用快捷键总结
- 创建制图表达规则
- SQL查询太长的问题
- This text field does not specify an inputType or a hint
- log4net创建日志
- 【Linux】在centOS上安装MySQL, JDK, Jboss
- 一些有用的网站。
- 平面最近点对