Android手机拍照后存储高清原图

Android手机中调用系统相机进行拍照的操作很简单:
1.调用系统相机

Intent openCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);  
startActivityForResult(openCameraIntent, TAKE_PICTURE);  

2.在回调中处理相机返回的图片

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (data.getExtras().containsKey("data")) {
        //取出返回的Bitmap数据
        Bitmap bitmap = (Bitmap) data.getExtras().get("data");
        //对bitmap做系列的操作,比如显示到ImageView、保存为文件等
        //省略操作。。。
    }
}

通过这种方法,显示或保存后的图片非常模糊,达不到项目的要求,这是因为系统返回的图片为缩略图。

所以说对于图片清晰度要求较高的项目,建议采用下面的操作:
1.调用系统相机,并指定图片存储路径

Intent openCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//建立Intent
File imageFile = new File(Environment.getExternalStorageDirectory(),"image001.jpg");//新建图片image001.jpg
Uri imageUri = Uri.fromFile(imageFile );//取出图片uri
openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);//指定拍照后的图片输出为imageUri,即生成为image001.jpg
startActivityForResult(openCameraIntent, 1);//启动相机

2.处理回调

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 1) {
        //上一步中,拍照后的原图存为了image001.jpg,所以在该方法中可直接取image001.jpg文件进行操作
        //1.图片文件很大,直接显示图片需要做优化处理
        //2.上传文件直接上传即可 
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好,关于Android Studio拍照ImageView显示图片的问题,可以通过以下步骤来实现: 1. 在AndroidManifest.xml文件中添加相机和写入外部存储的权限: ``` <uses-permission android:name="android.permission.CAMERA"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> ``` 2. 在布局文件中添加一个ImageView来显示图片: ``` <ImageView android:id="@+id/imageView" android:layout_width="match_parent" android:layout_height="wrap_content" android:adjustViewBounds="true" android:contentDescription="@string/image_desc"/> ``` 3. 在Activity中添加一个按钮来触发拍照功能,同时在onCreate方法中获取ImageView的引用: ``` private ImageView imageView; private static final int REQUEST_IMAGE_CAPTURE = 1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageView = findViewById(R.id.imageView); Button button = findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { dispatchTakePictureIntent(); } }); } ``` 4. 在Activity中添加拍照的处理逻辑,使用Intent来调用系统自带的相机应用,拍完照后将照片保存到指定路径,并显示在ImageView上: ``` private File photoFile = null; private String currentPhotoPath = null; private void dispatchTakePictureIntent() { Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); if (takePictureIntent.resolveActivity(getPackageManager()) != null) { try { photoFile = createImageFile(); } catch (IOException ex) { // Error occurred while creating the File ex.printStackTrace(); } if (photoFile != null) { Uri photoURI = FileProvider.getUriForFile(this, "com.example.android.fileprovider", photoFile); takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI); startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE); } } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) { // Set the image to ImageView setPic(); } } private File createImageFile() throws IOException { // Create an image file name String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date()); String imageFileName = "JPEG_" + timeStamp + "_"; File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES); File image = File.createTempFile( imageFileName, /* prefix */ ".jpg", /* suffix */ storageDir /* directory */ ); // Save a file: path for use with ACTION_VIEW intents currentPhotoPath = image.getAbsolutePath(); return image; } private void setPic() { // Get the dimensions of the View int targetW = imageView.getWidth(); int targetH = imageView.getHeight(); // Get the dimensions of the bitmap BitmapFactory.Options bmOptions = new BitmapFactory.Options(); bmOptions.inJustDecodeBounds = true; BitmapFactory.decodeFile(currentPhotoPath, bmOptions); int photoW = bmOptions.outWidth; int photoH = bmOptions.outHeight; // Determine how much to scale down the image int scaleFactor = Math.min(photoW / targetW, photoH / targetH); // Decode the image file into a Bitmap sized to fill the View bmOptions.inJustDecodeBounds = false; bmOptions.inSampleSize = scaleFactor; bmOptions.inPurgeable = true; Bitmap bitmap = BitmapFactory.decodeFile(currentPhotoPath, bmOptions); imageView.setImageBitmap(bitmap); } ``` 以上就是通过Android Studio实现拍照ImageView显示图片的步骤,如果还有问题可以继续提问哦。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值