首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android从byte[]数据中提取EXIF元数据

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

Stack Overflow用户
提问于 2013-11-20 17:53:11
回答 1查看 9.3K关注 0票数 4

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

下面是我的OnPictureTaken方法:

代码语言:javascript
复制
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);

    }
};

下面是我的执行代码:

代码语言:javascript
复制
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方法是:

代码语言:javascript
复制
public static String byteArrayToString(byte[] bytes)
{
    return new String(bytes);
}

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-11-20 19:01:03

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

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

  • Step 1.从文件路径中的摄像头获取字节数组2. onPictureTaken在上创建文件并将字节数组写入SDCard 3.从文件路径读取Exif元数据

@Override public void onPictureTaken(byte[]数据,摄像头){//步骤1.在新建文件sdDir = SDCard文件pictureFileDir =新建文件(sdDir,“RDCCameraImages”)上创建存储图像数据的文件;if (!pictureFileDir.exists() && !pictureFileDir.mkdirs()) { Log.d(TAG,“无法创建保存图像的目录”);返回;}//步骤2.将图片字节数组写入文件字符串photoFile = "Picture_“+ date + ".jpg";字符串imageFilePath = pictureFileDir.getPath() + File.separator + photoFile;文件pictureFile =新建文件(ImageFilePath);尝试{ FileOutputStream fos =新建FileOutputStream(pictureFile);fos.write(数据);fos.close();Toast.makeText(context,“保存的新图像:”+ photoFile,Toast.LENGTH_LONG).show();} catch (异常错误){ Log.d(TAG,“文件”+文件名+“未保存:”+ error.getMessage());Toast.makeText(context,“无法保存图像。”,Toast.LENGTH_LONG).show();}//步骤3.从文件路径ExifInterface Exif获取exif信息;尝试{exif=新建ExifInterface(imageFilePath);String make = exif.getAttribute(ExifInterface.TAG_MAKE);} catch (IOException e) { e.printStackTrace();}//检查此处“make”的值

}

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20092539

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档