调用摄像头拍照并显示
2016-09-27 17:04
423 查看
package com.example.choosepictest; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Environment; import android.provider.MediaStore; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ImageButton; import android.widget.ImageView; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; public class MainActivity extends AppCompatActivity { public static final int TAKE_PHOTO = 1; public static final int CROP_PHOTO = 2 ; private Button takephoto; private ImageView picture ; private Uri imageUri ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); takephoto = (Button) findViewById(R.id.take_phone); picture = (ImageView) findViewById( R.id.picture) ; takephoto.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //创建File对象,用于存储拍照后的照片 File outputImage = new File(Environment.getExternalStorageDirectory(),"output_image.jpg"); System.out.println(Environment.getExternalStorageDirectory()); try { if(outputImage.exists()){ outputImage.delete(); } outputImage.createNewFile(); }catch (IOException e){ e.printStackTrace(); } imageUri = Uri.fromFile(outputImage); Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri); startActivityForResult(intent , TAKE_PHOTO);//需要返回结果判断拍照成功的故用这个方法 } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode){ case TAKE_PHOTO: if(resultCode == RESULT_OK){ Intent intent = new Intent("com.android.camera.action.CROP");//为系统剪裁的Action intent.setDataAndType(imageUri ,"image/*"); //设置剪裁的属性类型 intent.putExtra("scale",true);//scale为缩放的意思 intent.putExtra(MediaStore.EXTRA_OUTPUT , imageUri); startActivityForResult(intent , CROP_PHOTO); //需要返回结果来判断是否截图成功的故用这个方法 } break; case CROP_PHOTO: if(resultCode==RESULT_OK){ try{ Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));//而contentresolver则是用于管理所有程序的contentprovider实例 picture.setImageBitmap(bitmap); }catch (FileNotFoundException e){ e.printStackTrace(); } } break; default:break; } } }
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Take photo" android:id="@+id/take_phone"/> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/picture" android:layout_gravity="center_vertical"/> </LinearLayout>
总结:1 Environment.getExternalStorageDirectory() 得到SD卡的根目录
2 剪裁照片的属性设置
3 解析成位图
相关文章推荐
- android调用摄像头拍照并显示
- Android调用系统摄像头拍照并显示在ImageView上
- android 调用摄像头进行拍照,并对照片进行裁剪,然后显示。
- Android调用系统摄像头拍照并显示在ImageView上
- android调用摄像头拍照,并且用imageView显示出来
- HTML5调用手机前置摄像头或后置摄像头拍照,canvas显示,经过Android测试
- matlab调用下摄像头,用GUI界面显示摄像头的画面并实现拍照
- 调用系统摄像头拍照,对拍照后的图片进行裁剪和压缩处理,并显示在imageview上面
- 《OpenCV》Part2 OpenCV3.1.0调用摄像头显示视频并拍照
- FootStep 40、如何调用S60的系统摄像头功能进行拍照与摄像?
- VB 调用摄像头拍照,保存。
- iPhone开发 调用摄像头进行拍照等操作
- 通过调用系统摄像头拍照,拍视频,然后写入文件的代码范例
- Wp7: 调用摄像头拍照并上传图片(完整版)
- jsp调用摄像头拍照,控制摄像头操作
- FootStep 40、如何调用S60的系统摄像头功能进行拍照与摄像?
- iPhone开发 调用摄像头进行拍照等操作
- 主题:android调用系统摄像头拍照图片和视频
- 调用摄像头拍照或者摄像,把获得的数据写入到文件
- Java调用摄像头进行拍照源码,并且有JMF下载地址