将元数据添加到Android中创建的位图 [英] Add Metadata to created bitmap in Android

查看:121
本文介绍了将元数据添加到Android中创建的位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用相机意图拍照,然后将图像转换为字节数组,最终将其保存在本地数据库中.相机拍摄的原始图像具有所有元数据,例如GPS纬度和经度等.但是,我根据该图像创建的位图不包含任何元数据.如何将原始的metedata添加到我的图像中?

I am using a camera intent to take pictures, then I convert that image to a bytearray and eventually save it locally in my database. The original image taken with the camera has all the metadata like GPS latitude and longtitude etc. However, the bitmap I create from that image does not include any meta data. How can I add the original metedata to my image ?

这是我的代码,其中摄影机意图返回了:

Here is my code where the camera intent returns:

picUri = data.getData()    
Bitmap yourSelectedImage62 = null;
                String imagebytes2 ;
                try
                {
                    yourSelectedImage62 = MediaStore.Images.Media.getBitmap(this.getContentResolver(), picUri);
                    yourSelectedImage62 = Bitmap.createScaledBitmap(yourSelectedImage62, large_width, large_height, true);
                } catch (FileNotFoundException ex)
                {
                    Logger.getLogger(Screen_View_Submission.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IOException ex) {
                    Logger.getLogger(Screen_New_Submission.class.getName()).log(Level.SEVERE, null, ex);
                }

                ByteArrayOutputStream bao62 = new ByteArrayOutputStream();
                yourSelectedImage62.compress(Bitmap.CompressFormat.JPEG, 75, bao62);
                byte[] ba62 = bao62.toByteArray();
                imagebytes2 = Base64.encodeToString(ba62,Base64.DEFAULT);
                yourSelectedImage62.recycle();

推荐答案

元数据不是位图的属性.它存储为JPEG文件的Exinf.使用 ExifInterface 更新元数据,这是设置gps坐标的示例

Meta data is not a property of bitmap. it is stored as Exinf of JPEG file. use ExifInterface to update meta data here is an example to set gps cordinates

exif = new ExifInterface(imageFile.getAbsolutePath());
        int num1Lat = (int) Math.floor(latitude);
        int num2Lat = (int) Math.floor((latitude - num1Lat) * 60);
        double num3Lat = (latitude - ((double) num1Lat + ((double) num2Lat / 60))) * 3600000;

        int num1Lon = (int) Math.floor(longitude);
        int num2Lon = (int) Math.floor((longitude - num1Lon) * 60);
        double num3Lon = (longitude - ((double) num1Lon + ((double) num2Lon / 60))) * 3600000;

        exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE, num1Lat + "/1," + num2Lat + "/1," + num3Lat + "/1000");
        exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE, num1Lon + "/1," + num2Lon + "/1," + num3Lon + "/1000");

        if (latitude > 0) {
            exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE_REF, "N");
        } else {
            exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE_REF, "S");
        }

        if (longitude > 0) {
            exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF, "E");
        } else {
            exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF, "W");
        }
        exif.setAttribute(ExifInterface.TAG_MAKE, "FooBarr");
        exif.setAttribute(ExifInterface.TAG_MODEL, "KooKKoo");
        exif.setAttribute(ExifInterface.TAG_ORIENTATION,orientation+"");
        exif.saveAttributes();

这篇关于将元数据添加到Android中创建的位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆