【问题标题】:Android Extract EXIF metadata from byte[] dataAndroid 从 byte[] 数据中提取 EXIF 元数据
【发布时间】:2016-07-12 06:31:44
【问题描述】:

我有一个自定义相机应用程序。我需要自定义相机捕获的图像元数据。我在 decodebytearray (Constant.imageData1 = data;) 之前保存了字节数据,并将其保存到类型为 byte 的常量类中,在使用此字节数据之前,我将其转换为字符串。当我要使用 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);

    }
};

这是我的执行代码:

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

而bytearraytostring方法是:

public static String byteArrayToString(byte[] bytes)
{
    return new String(bytes);
}

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

【问题讨论】:

  • 为什么你需要Exif信息,只是因为图像旋转,或者其他?让我知道我做过同样的工作,会指导你。
  • @RDC,实际上我需要这个,因为我必须在 logcat(log.e) 中显示该图像的“MAKE、MODEL 和 FOCAL LENGTH”。
  • 好的,您可能需要使用 3rd pary api 来从字节数组中获取 Image Exif 元数据.. 这里stackoverflow.com/questions/12944123/…
  • @RDC,我不想使用第三方 api,还有其他方法可以得到吗??????请帮帮我。
  • 遗憾的是,Android Api 不允许您从字节数组 Stream 中读取 exif 数据,只能从文件中读取。因此您必须将字节数组写入文件,然后才能读取 Exif 信息。

标签: android metadata android-camera exif


【解决方案1】:

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

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

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

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

    }

【讨论】:

  • 非常感谢您的回答。问题是我在图片回调中使用的“旋转位图”。现在它工作正常。
  • @RDC 请把变量名ex改成exif
  • @RDC 你在 catch 语句下面还有一个额外的尖括号
  • 我写了一篇关于reading and writing EXIF的文章到android中的一个图像文件中。我希望这会有所帮助。
  • @Shailendra 您的文章和您的网站已过期
猜你喜欢
  • 1970-01-01
  • 2013-05-14
  • 1970-01-01
  • 2018-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-11
相关资源
最近更新 更多