Android拍照Camera(二)
来源:互联网 发布:中企动力域名转出 编辑:程序博客网 时间:2024/04/29 20:31
<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> <ImageView android:id="@+id/img" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/btn" android:scaleType="center" > </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_CROP = 0x12;ImageView img;Uri imgUri;@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);btn.setOnClickListener(this);}@Overridepublic void onClick(View v) {if (v.getId() == R.id.btn) {takePicture();}}/** * 拍摄照片并保存 */public void takePicture() {String 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);imgUri = Uri.fromFile(file);intent.putExtra(MediaStore.EXTRA_OUTPUT, imgUri);startActivityForResult(intent, REQUEST_CODE_IMAGE_CATURE);}/** * 裁剪图片 */public void cropPicture() {Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(imgUri, "image/*"); intent.putExtra("scale", true); intent.putExtra("outputX", 300); intent.putExtra("outputY", 300); intent.putExtra(MediaStore.EXTRA_OUTPUT, imgUri); startActivityForResult(intent, REQUEST_CODE_IMAGE_CATURE_CROP); }@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) {cropPicture();} else if (requestCode == REQUEST_CODE_IMAGE_CATURE_CROP) {InputStream is = null;try { // 根据文件Uri,读取输入流is = getContentResolver().openInputStream(imgUri);Bitmap bitmap = BitmapFactory.decodeStream(is);img.setImageBitmap(bitmap);} catch (FileNotFoundException e) {e.printStackTrace();}}}}
0 0
- Android拍照Camera(二)
- android Camera照相机技术(二)定制拍照
- Android扩展 - 拍照篇(Camera)
- Android扩展 - 拍照篇(Camera)
- Android Camera (一)通过Intent 拍照
- 启动系统Camera拍照(Android)
- android camera 后台拍照
- Android Camera进行拍照
- Android之Camera拍照
- android Camera拍照
- Android Camera进行拍照
- Android+Camera+拍照
- Android Camera拍照实现
- Android Camera 无法拍照
- Android Camera拍照
- Android之Camera拍照
- Android照相机拍照Camera
- Android camera拍照分辨率
- 高并发(1)
- 《知识发现与智能决策》笔记
- HA_keepalived基础配置篇(一)
- Preprocessor Macros添加无效
- iOS开发 -- 常见坑(模拟器运行没有错误,真机运行有,但是还能运行, Not a PNG file..)
- Android拍照Camera(二)
- Notification bar icon turns white in Android 5 Lollipop
- oracle 数据库的建表以及字段的增删改查
- bzoj1925
- doT.js的基本使用
- javascript中的静态和非静态使用
- Android开发学习之路--异步消息Handler,Message,Looper和AsyncTask之初体验
- PHP编程效率的20个要点
- window和Linux双系统,如何卸载其中之一