减小DNG文件的大小

发布于 2025-02-07 21:46:53 字数 3938 浏览 4 评论 0原文

我正在尝试减少生成的DNG文件的文件大小。

 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;
}

这是我到目前为止尝试过的方法,转换为位图并将其写出为DNG文件。因此生成的文件是损坏的,我无法打开它。我相信它可能缺少DNG特定的元数据,但是我不确定使其正常工作的进一步步骤。任何帮助都将受到赞赏

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文