javaandroidbitmapandroid-image-capture

Google Mobile Vision Low Image Quality


I try to take photo from camera with mobile vision library but taken image's qualities are low (Resolution 240x320)

I don't know where I should modify this feature(I guess it is related with CameraSource) and I'm not sure I should modify or not.

Here is my Capture Function (found from here : How Capture Picture while mobile vision api - face tracking )

public void takePicture(){

        cameraSource.takePicture(null, new CameraSource.PictureCallback() {
            private File imageFile;

            @Override
            public void onPictureTaken(byte[] bytes) {
                try {
                    // convert byte array into bitmap
                    Bitmap loadedImage = null;
                    Bitmap rotatedBitmap = null;
                    loadedImage = BitmapFactory.decodeByteArray(bytes, 0,
                            bytes.length);

                    Matrix rotateMatrix = new Matrix();
                    rotateMatrix.postRotate(0);
                    rotatedBitmap = Bitmap.createBitmap(loadedImage, 0, 0,
                            loadedImage.getWidth(), loadedImage.getHeight(),
                            rotateMatrix, false);

                    File dir = new File(
                            Environment.getExternalStoragePublicDirectory(
                                    Environment.DIRECTORY_PICTURES), "MyPhotos");

                    boolean success = true;
                    if (!dir.exists())
                    {
                        success = dir.mkdirs();
                    }

                    if (success) {
                        java.util.Date date = new java.util.Date();

                        imageFile = new File(dir.getAbsolutePath()
                                + File.separator
                                + new Timestamp(date.getTime()).toString()
                                + "Image.jpg");

                        pictureName = imageFile.getName();
                        Log.i("Picture Name ", pictureName);
                        imageFile.createNewFile();

                        Toast.makeText(MainActivity.this, "Image Saved", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(getBaseContext(), "Image Not saved",
                                Toast.LENGTH_SHORT).show();
                        return;
                    }
                    ByteArrayOutputStream ostream = new ByteArrayOutputStream();

                    // save image into gallery
                    rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, ostream);

                    FileOutputStream fout = new FileOutputStream(imageFile);
                    fout.write(ostream.toByteArray());
                    fout.close();
                    ContentValues values = new ContentValues();

                    values.put(MediaStore.Images.Media.DATE_TAKEN,
                            System.currentTimeMillis());
                    values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
                    values.put(MediaStore.MediaColumns.DATA,
                            imageFile.getAbsolutePath());

                    MainActivity.this.getContentResolver().insert(
                            MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

                    //saveToInternalStorage(loadedImage);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

Here is CameraSource creation:

        cameraSource = new CameraSource.Builder(getApplicationContext(), detector)
                .setFacing(CameraSource.CAMERA_FACING_BACK)
                .setRequestedPreviewSize(1280, 1024)
                .setRequestedFps(2.0f)
                .setAutoFocusEnabled(true)
                .build();

Should I modify anything or add something in here?

Thanks for help.


Solution

  • I fix this problem with changing these two lines:

     rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, ostream);
    

    I changed format to PNG

    rotatedBitmap.compress(Bitmap.CompressFormat.PNG, 100, ostream);
    

    And changed these attributes of camerasource when I create the variable

    .setRequestedPreviewSize(1920, 1024)
     .setRequestedFps(3.0f)