React Native读取二进制数据

3
我有一个React Native应用程序,它加载包含JPG数据的二进制文件。我将三个JPG图像打包到二进制文件中。在该文件的开头,我放置了一些额外信息:
  1. 总JPG文件数
  2. 图像1文件大小
  3. 图像2文件大小
  4. 图像3文件大小
我可以使用fetch获取二进制数据并获得Blob对象。但是我如何从二进制文件中读取这些额外信息呢?
图片:enter image description here
1个回答

2

最终我自己完成了它

import React, { Component, useState } from 'react'
import { View, Button, Text, Image } from 'react-native'

export const TestScreen = ({ navigation }) => {
    const [base64data, setBase64data] = useState(undefined)

    let previewBase64 = []

    const appUpdate = async () => {
        fetch('preview.data').then(
            async (data) => {
                let binary = await data.blob()
                
                //read 10 bytes of file header
                let bytes = binary.slice(0, 10)
                blob2base64(bytes).then(async (data) => {
                    const bytes = base642bytes(data)
                    const totalImages = bytes[0]

                    let imageBytesOffset = 0
                    for (let i = 0; i < totalImages; i++) {
                        const imageSize = sumBytes(
                            bytes[i * 3 + 1],
                            bytes[i * 3 + 2],
                            bytes[i * 3 + 3]
                        )

                        const start = 10 + imageBytesOffset
                        const end = 10 + imageSize + imageBytesOffset
                        const imageBlob = binary.slice(start, end)
                        //adding new image to array
                        previewBase64.push(await blob2base64(imageBlob))
                        imageBytesOffset += imageSize

                        console.log(imageSize)
                    }
                    //loading 3d image from binary file
                    setBase64data(previewBase64[2])
                })
            }
        )
    }

    const blob2base64 = (value) => {
        return new Promise((resolve, reject) => {
            const fileReaderInstance = new FileReader()
            fileReaderInstance.readAsDataURL(value)
            fileReaderInstance.onload = () => {
                resolve(fileReaderInstance.result)
            }
        })
    }

    const base642bytes = (value) => {
        const content = atob(
            value.substr('data:application/octet-stream;base64,'.length)
        )

        const buffer = new ArrayBuffer(content.length)
        const view = new Uint8Array(buffer)
        view.set(Array.from(content).map((c) => c.charCodeAt(0)))
        return view
    }

    const sumBytes = (byte1, byte2, byte3) => {
        let bytes = new Uint8Array(3)
        bytes[0] = byte1
        bytes[1] = byte2
        bytes[2] = byte3

        const result =
            ((bytes[0] & 0xff) << 16) |
            ((bytes[1] & 0xff) << 8) |
            (bytes[2] & 0xff)

        return result
    }

    const chars =
        'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='
    const atob = (input = '') => {
        let str = input.replace(/=+$/, '')
        let output = ''

        if (str.length % 4 == 1) {
            throw new Error(
                "'atob' failed: The string to be decoded is not correctly encoded."
            )
        }
        for (
            let bc = 0, bs = 0, buffer, i = 0;
            (buffer = str.charAt(i++));
            ~buffer && ((bs = bc % 4 ? bs * 64 + buffer : buffer), bc++ % 4)
                ? (output += String.fromCharCode(255 & (bs >> ((-2 * bc) & 6))))
                : 0
        ) {
            buffer = chars.indexOf(buffer)
        }

        return output
    }

    console.log('TestScreen')

    return (
        <View style={{ backgroundColor: '#FFFFFF', flex: 1, paddingTop: 60 }}>
            <Button
                title="Update app"
                color="#000000"
                onPress={appUpdate}
            ></Button>
            <Image
                source={{ uri: base64data }}
                style={{
                    height: 200,
                    width: null,
                    flex: 1,
                    resizeMode: 'contain',
                }}
            />
        </View>
    )
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接