Android 从 byte[] 数据中提取 EXIF 元数据

Moh*_*job 4 android exif metadata android-camera

我有一个自定义相机应用程序。我需要自定义相机捕获的图像的元数据。我在解码字节数组(Constant.imageData1 = data;)之前保存了字节数据,并将其保存到字节类型的常量类中,在使用此字节数据之前,我将其转换为字符串。当我要使用 ExifInterface 执行它并将其显示到日志时,应用程序崩溃了。

这是我的 OnPictureTaken 方法:

PictureCallback mPicture = new PictureCallback() {
    @Override
    public void onPictureTaken(byte[] data, Camera camera) {
        Constant.imageData1 = data;
        Log.e("Camrera", "22222222222222222");
        BitmapFactory.Options bfo = new BitmapFactory.Options();
        bfo.inDither = false;
        // bfo.inJustDecodeBounds = true;
        bfo.inPurgeable = true;
        bfo.inTempStorage = new byte[16 * 1024];

        Intent intent = new Intent(context, PreviewActivity.class);
        // intent.putExtra("data", data);
        Bitmap bitmapPicture = BitmapFactory.decodeByteArray(data, 0,
                data.length, bfo);
        Matrix matrix = new Matrix();
        if (Constant.result == 180) {
            matrix.postRotate(270);
        }
        if (Constant.result == 270) {
            matrix.postRotate(180);
        }
        int height = bitmapPicture.getHeight();
        int width = bitmapPicture.getWidth();
        //Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmapPicture,
                //height, width, true);
        Bitmap rotatedBitmap = Bitmap.createBitmap(bitmapPicture, 0, 0,
                bitmapPicture.getWidth(), bitmapPicture.getHeight(), matrix,
                true);
        ByteArrayOutputStream blob = new ByteArrayOutputStream();
        Log.e("Camrera1", "22222222222222222");
        rotatedBitmap.compress(CompressFormat.JPEG,
                50 /* ignored for PNG */, blob);
        byte[] bitmapdata = blob.toByteArray();
        Constant.imageData = bitmapdata;
        Log.e("Camrera2", "22222222222222222");
        startActivity(intent);

    }
};
Run Code Online (Sandbox Code Playgroud)

这是我的执行代码:

private void SaveImage() {
    try {
        String data = byteArrayToString(Constant.imageData1);
        ExifInterface ex = new ExifInterface(data);
        String make = ex.getAttribute(ExifInterface.TAG_MAKE);
        Log.e("Make", make);
        Log.e("Make", make);
        Log.e("Make", make);
        finish();

    } catch (Exception e) {
            e.printStackTrace();
        }
}
Run Code Online (Sandbox Code Playgroud)

bytearraytostring 方法是:

public static String byteArrayToString(byte[] bytes)
{
    return new String(bytes);
}
Run Code Online (Sandbox Code Playgroud)

这对我来说非常重要。请帮我。

swi*_*Boy 5

请尝试下面的代码片段,并在需要的地方进行更改。警告:我还没有测试过。

\n\n

所以,基本上,我所做的是:

\n\n
    \n
  • 步骤1.在onPictureTaken方法中从相机获取字节数组。
  • \n
  • 步骤2.在SDCard上创建文件并将字节数组写入文件
  • \n
  • 步骤 3.从文件路径读取 Exif 元数据

    \n\n
    @Override\npublic void onPictureTaken(byte[] data, Camera camera) {\n\n    //Step 1. Create file for storing image data on SDCard\n    File sdDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);\n    File pictureFileDir = new File(sdDir, "RDCCameraImages");\n\n    if (!pictureFileDir.exists() && !pictureFileDir.mkdirs()) {\n\n      Log.d(TAG, "Can\'t create directory to save image.");\n      return;\n\n    }\n\n    //Step 2. write image byte array to file\n    String photoFile = "Picture_" + date + ".jpg";\n    String imageFilePath = pictureFileDir.getPath() + File.separator + photoFile;\n    File pictureFile = new File(imageFilePath);\n\n    try \n    {\n          FileOutputStream fos = new FileOutputStream(pictureFile);\n          fos.write(data);\n          fos.close();\n          Toast.makeText(context, "New Image saved:" + photoFile,\n              Toast.LENGTH_LONG).show();\n        } catch (Exception error) {\n          Log.d(TAG, "File" + filename + "not saved: "\n              + error.getMessage());\n          Toast.makeText(context, "Image could not be saved.",\n              Toast.LENGTH_LONG).show();\n    }           \n    //Step 3. Get Exif Info from File path\n    ExifInterface exif;\n    try {\n        exif = new ExifInterface(imageFilePath);\n        String make = exif.getAttribute(ExifInterface.TAG_MAKE);\n\n    } catch (IOException e) {\n        e.printStackTrace();\n    }\n\n    //check the value of  \xe2\x80\x9cmake\xe2\x80\x9d here\n
    Run Code Online (Sandbox Code Playgroud)\n\n

    }

  • \n
\n