直接调用手机摄像头拍照保存到本地,图片很模糊该怎么解决?

1.调用摄像头

 takePhoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(cameraIntent, TAKE_PHOTO);
            }
        });

2.Bitmap转File

 public File saveBitmapFile(Bitmap bitmap) {
//        File file = new File(getExternalCacheDir(), "temp.jpg");//将要保存图片的路径
        File file = new File(getExternalCacheDir(), "temp.jpg");//将要保存图片的路径
        try {
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
//            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
            bos.flush();
            bos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return file;
    }

3.获取结果

   @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (data != null) {
            if (requestCode == TAKE_PHOTO && resultCode == RESULT_OK) {
                Bitmap photo = (Bitmap) data.getExtras().get("data");
                picture.setImageBitmap(photo);
                File file = saveBitmapFile(photo);
                String absolutePath = file.getAbsolutePath();
                Log.d(TAG, "onActivityResult: " + absolutePath);
            }
        }
    }
    
阅读 2.1k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题