danny
danny

Reputation: 1

Reducing size of a DNG file

I am trying to reduce the file size of a generated DNG file.

 void compressImage(String path) {
    Bitmap scaledBitmap = null;
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    Bitmap bmp = BitmapFactory.decodeFile(path, options);

    int actualHeight = options.outHeight;
    int actualWidth = options.outWidth;
    float maxHeight = 816.0f;
    float maxWidth = 612.0f;
    float imgRatio = (float) (actualWidth * 1.0 / actualHeight);
    float maxRatio = maxWidth / maxHeight;

    /* Setting height and width */
    if (actualHeight > maxHeight || actualWidth > maxWidth) {
        if (imgRatio < maxRatio) {
            imgRatio = maxHeight / actualHeight;
            actualWidth = (int) (imgRatio * actualWidth);
            actualHeight = (int) maxHeight;
        } else if (imgRatio > maxRatio) {
            imgRatio = maxWidth / actualWidth;
            actualHeight = (int) (imgRatio * actualHeight);
            actualWidth = (int) maxWidth;
        } else {
            actualHeight = (int) maxHeight;
            actualWidth = (int) maxWidth;

        }
    }

    options.inSampleSize = calculateInSampleSize(options, actualWidth, actualHeight);
    options.inJustDecodeBounds = false;
    options.inTempStorage = new byte[16 * 1024];

    /* Creating scaled bitmap */
    try {
        bmp = BitmapFactory.decodeFile(path, options);
        scaledBitmap = Bitmap.createBitmap(actualWidth, actualHeight, Bitmap.Config.ARGB_8888);
    } catch (OutOfMemoryError exception) {
        exception.printStackTrace();
        exception.printStackTrace();
    }
    float ratioX = actualWidth / (float) options.outWidth;
    float ratioY = actualHeight / (float) options.outHeight;
    float middleX = actualWidth / 2.0f;
    float middleY = actualHeight / 2.0f;

    /* Creating a scale matrix*/
    Matrix scaleMatrix = new Matrix();
    scaleMatrix.setScale(ratioX, ratioY, middleX, middleY);

    /* Drawing bitmap on canvas */
    Canvas canvas;
    if (scaledBitmap != null) {
        canvas = new Canvas(scaledBitmap);
        canvas.setMatrix(scaleMatrix);
        canvas.drawBitmap(bmp, middleX - (bmp.getWidth() >> 1), middleY - (bmp.getHeight() >> 1), new Paint(Paint.FILTER_BITMAP_FLAG));
    }

    if (scaledBitmap != null) {

        int size = scaledBitmap.getRowBytes() * scaledBitmap.getHeight();
        ByteBuffer buffer = ByteBuffer.allocate(size);
        scaledBitmap.copyPixelsToBuffer(buffer);
        byte[] byteArray = buffer.array();

        /* writing to file */
        try {
            File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath() + "/sampleCheck.dng");
            if (!file.exists()) {
                file.createNewFile();
            }
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(byteArray);
            fos.close();
            /* Open the file  */
        } catch (Exception e) {
            Log.d(TAG, "compressImage: " + e.getMessage());
        }
    }

}

private static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {
        final int heightRatio = Math.round((float) height / (float) reqHeight);
        final int widthRatio = Math.round((float) width / (float) reqWidth);
        inSampleSize = Math.min(heightRatio, widthRatio);
    }
    final float totalPixels = width * height;
    final float totalReqPixelsCap = reqWidth * reqHeight * 2;

    while (totalPixels / (inSampleSize * inSampleSize) > totalReqPixelsCap) {
        inSampleSize++;
    }

    return inSampleSize;
}

This is what I have tried so far, converted to a bitmap and writing it out as a DNG file. The file thus generated is corrupt and I'm unable to open it. I believe it might be missing DNG specific meta data, but I am unsure on further steps to make it work. Any help is appreciated

Upvotes: 0

Views: 93

Answers (0)

Related Questions