有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java为Android中的jpeg文件设置DPI元信息

在我正在编写的一个Android应用程序中,我有一个文档图像(jpeg),它被上传到一个服务器上,该服务器可以识别文档并将相关细节发送给我。虽然这一切都很好,但服务器中的代码希望我像mac那样设置“图像DPI”元信息

Screen-shot of the meta-information of an image

上面截图中显示的“图像DPI”并不完全是它的值。我写了一个计算dpi值的方法。如何将计算出的dpi值设置为我的jpeg文档的元信息?我已经能够在应用程序的iOS对应程序中设置这个特定的元信息,但在安卓系统中,两天的不懈尝试让我的差事徒劳无功

我确实知道ExifInterface,而且我使用它的setAttribute(String key,String value)方法很不走运。(键应该是什么?值应该是什么?如何设置单位?我应该设置单位吗?)

我还看到了与Java相关的解决方案,建议使用javax.imageio.*包,这是Android无法使用的

有人遇到过这样的问题吗?我该如何处理这个问题


共 (1) 个答案

  1. # 1 楼答案

    要编辑该值,首先需要创建一个byte[]数组来存储Bitmap.compress()。这是我的代码的一部分,我就是这么做的(输入是源位图)

    ByteArrayOutputStream uploadImageByteArray = new ByteArrayOutputStream();
    input.compress(Bitmap.CompressFormat.JPEG, 100, uploadImageByteArray);
    byte[] uploadImageData = uploadImageByteArray.toByteArray();
    

    根据JFIF结构,需要编辑字节数组中的第13、14、15、16和17个索引。第13位指定密度类型,第14位和第15位指定X分辨率,第16位和第17位指定Y分辨率。我通过以下方法获得了dpi:

    private long getDPIinFloat(int width, int height) {
            return (long) Math.sqrt(width * width + height * height) / 4;
    }
    

    在我拿到DPI后,我不得不做一些操作,比如:

    long firstPart = dpiInFloat >> 8;
    if (GlobalState.debugModeOn) {
        Log.d(TAG, "First Part: " + firstPart);
    }
    long lastPart = dpiInFloat & 0xff;
    if (GlobalState.debugModeOn) {
        Log.d(TAG, "Last Part: " + lastPart);
    }
    

    然后,像这样操作字节信息:

    uploadImageData[13] = 1;
    uploadImageData[14] = (byte) firstPart;
    uploadImageData[15] = (byte) lastPart;
    uploadImageData[16] = (byte) firstPart;
    uploadImageData[17] = (byte) lastPart;
     //Upload Image data to the server
    

    这样,我就可以在元数据上设置dpi信息