-3

I integrated camera in my application and using that camera the captured image is blur. Any suggestions for improving captured image quality. I am using multipart for sending image on server.

Code Snippet

@Override
public void openCameraAction() {
    Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(intent, CAMERA_REQUEST);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == Constants.CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
        photo = (Bitmap) data.getExtras().get("data");

        imageBase64 = encodeToBase64(photo);

        imageUri = getImageUri(getApplicationContext(), photo);
        imageFile = new File(getRealPathFromURI(imageUri));

        getImageView.setImageBitmap(photo);

        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"),imageFile);
        MultipartBody.Part image = MultipartBody.Part.createFormData(imageQuestionId, imageFile.getName(),requestBody);
        parts.add(image);
    }
}

public static String encodeToBase64(Bitmap image)
{
    Bitmap immagex=image;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    immagex.compress(Bitmap.CompressFormat.PNG, 100, baos);
    byte[] b = baos.toByteArray();
    String imageEncoded = Base64.encodeToString(b, Base64.DEFAULT);
    return imageEncoded;
}

public Uri getImageUri(Context inContext, Bitmap inImage) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
    return Uri.parse(path);
}

public String getRealPathFromURI(Uri uri) {
    Cursor cursor = getContentResolver().query(uri, null, null, null, null);
    cursor.moveToFirst();
    int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
    return cursor.getString(idx);
}
Garrima Kakkr
  • 15
  • 2
  • 5

2 Answers2

0

Find a class image picker in this gist https://gist.github.com/r00786/2c9aa88b706daccd55098ac2c28e7f39

all the things are handled in this class

How to use

 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;
    }
  }
Rohit Sharma
  • 1,384
  • 12
  • 19
0

If you want the actual image taken camera to server u need to create the image

Try this code

Delcare this Variable

private String actualPictureImagePath = "";

Then call this method on button click 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);
}

and Then in onActivityResult() handle this

@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();

            }
        }

    }

This is the code to use for Bitmap to 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);

NOTE:-

Do not forget to Add runtime permissions and in manifest also

1)Read and Write Persmission

2)Camera persmission

Quick learner
  • 10,632
  • 4
  • 45
  • 55