简体   繁体   English

如何从 Firebird blob 数据创建图像文件?

[英]How to create an image file from Firebird blob data?

I have a Firebird DB table, which contains fields with image data (as blob sybtype -5).我有一个 Firebird 数据库表,其中包含带有图像数据的字段(如 blob sybtype -5)。 I need to get this data and convert it to an image file.我需要获取这些数据并将其转换为图像文件。 Code example below creates a file (but size is very large, and while trying to open it - it is said, that file is not right BMP file).下面的代码示例创建了一个文件(但大小非常大,并且在尝试打开它时 - 据说该文件不是正确的 BMP 文件)。 What am I missing here?我在这里错过了什么?

result[0].OBJ_IMAGE1((err, name, eventEmitter) => {
    if(err) throw err;

    let chunks = [];
    eventEmitter.on('data', chunk => {
        chunks.push(chunk);                
    });
    eventEmitter.once('end', () => {
        let buffer = Buffer.concat(chunks);         

        fs.writeFile('test.png',  buffer, err => {
            if(err) throw err;
            console.log('image file written');
        });            
    });
});

在此处输入图像描述

在此处输入图像描述

I have tested your code, and it will correctly write out the blob to a file.我已经测试了您的代码,它会正确地将 blob 写入文件。 Your problem is therefore - as also discussed in the comments - one of not knowing what the actual image file type is, so you're storing it with the wrong file extension.因此,您的问题是 - 正如评论中所讨论的那样 - 不知道实际图像文件类型是什么,因此您使用错误的文件扩展名存储它。 You need to know the file type (or encoding, or encryption) used by the application storing the image data to be able to select the right file extension, image viewer to display the data and/or additional transformations required to get a viewable file.您需要知道存储图像数据的应用程序使用的文件类型(或编码或加密)才能 select 正确的文件扩展名、图像查看器以显示数据和/或获取可查看文件所需的其他转换。

Looking at the example data and the File Signatures link Marcodor provided, it is possible that the stored data is type "Standard JPEG file with Exif metadata" ( FF D8 FF E1 xx xx 45 78 69 66 00 ), but the file is prefixed with 5 bytes of application specific data (maybe some sort of type identifier, or a file length, or something like that).查看示例数据和 Marcodor 提供的文件签名链接,存储的数据可能是“带 Exif 元数据的标准 JPEG 文件”类型( FF D8 FF E1 xx xx 45 78 69 66 00 ),但文件前缀为5 个字节的应用程序特定数据(可能是某种类型标识符,或文件长度,或类似的东西)。 Based on that, try using SUBSTRING(OBJ_IMAGE1 FROM 6) and see if saving as jpg allows you to open the file (NOTE: This doesn't necessarily mean all files stored in these blobs are JPEGs.).基于此,尝试使用SUBSTRING(OBJ_IMAGE1 FROM 6)并查看是否保存为jpg允许您打开文件(注意:这并不一定意味着存储在这些 blob 中的所有文件都是 JPEG。)。

As an aside, it is more efficient to store the image using:顺便说一句,使用以下方法存储图像更有效:

result[0].OBJ_IMAGE1((err, name, e) => {
    if (err) throw err;
    var imgStream = fs.createWriteStream('filename.ext')
    e.pipe(imgStream);
});

Using pipe will write the chunks directly to a file as they are received, instead of accumulating the entire file into memory before writing it out.使用pipe会将接收到的块直接写入文件,而不是在写出之前将整个文件累积到 memory 中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM