在Android应用中提高以编程方式捕获的图像的质量

时间:2018-08-07 12:04:23

标签: android android-camera

我在应用程序中集成了相机,使用该相机拍摄的图像模糊。有关改善捕获图像质量的任何建议。 我正在使用multipart在服务器上发送图像。

  

代码段

order by coalesce(year, 0)

2 个答案:

答案 0 :(得分:0)

在此要点中查找类图像选择器 https://gist.github.com/r00786/2c9aa88b706daccd55098ac2c28e7f39

所有事情都在此类中处理

使用方法

 private static final int PICK_IMAGE_ID = 234; // the number doesn't matter

  public void onPickImage(View view) {
    Intent chooseImageIntent = ImagePicker.getPickImageIntent(this);
    startActivityForResult(chooseImageIntent, PICK_IMAGE_ID);
  }

  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch(requestCode) {
        case PICK_IMAGE_ID:
            Bitmap bitmap = ImagePicker.getImageFromResult(this, resultCode, data);
            // TODO use bitmap
            break;
        default:
            super.onActivityResult(requestCode, resultCode, data);
            break;
    }
  }

答案 1 :(得分:0)

  

如果要将实际拍摄的相机图像拍摄到服务器,则需要创建   图片

尝试此代码

Delcare这个变量

private String actualPictureImagePath = "";

然后在点击按钮cameraIntent()上调用此方法

private void cameraIntent() {
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = timeStamp + ".jpg";
    File storageDir = Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES);
    actualPictureImagePath = storageDir.getAbsolutePath() + "/" + imageFileName;
    File file = new File(pictureImagePath);
    Uri outputFileUri = Uri.fromFile(file);
    Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);               
    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
    startActivityForResult(cameraIntent, 1);
}

然后在onActivityResult()中处理

@override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 1) {
        File imgFile = new  File(actualPictureImagePath);
            if(imgFile.exists()){        
          InputStream inputStream = null;//You can get an inputStream using any IO API
inputStream = new FileInputStream(imgFile.getAbsolutePath());
byte[] buffer = new byte[8192];
int bytesRead;
ByteArrayOutputStream output = new ByteArrayOutputStream();
Base64OutputStream output64 = new Base64OutputStream(output, Base64.DEFAULT);
try {
    while ((bytesRead = inputStream.read(buffer)) != -1) {
        output64.write(buffer, 0, bytesRead);
    }
} catch (IOException e) {
    e.printStackTrace();
}
output64.close();

String base64String = output.toString();

            }
        }

    }

这是用于位图到Base64的代码

ByteArrayOutputStream baos = new ByteArrayOutputStream();  
          myBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm 
          is the bitmap object   
          byte[] byteArrayImage = baos.toByteArray(); 
          String encodedImage = Base64.encodeToString(byteArrayImage, 
          Base64.DEFAULT);

注意:-

别忘了添加运行时权限,并且也在清单中

  

1)读写权限

     

2)相机权限