将十六进制字符串转换为图像文件[重复]

2024-01-07

我需要从收到的 xml 文件中读取表示 JPEG 文件的带有前导零的十六进制字符串,并将其保存为图像文件。图像数据看起来像

0000005000000050FF191818FF151715FF111413FF0E1...........................FF2A2322FF292221

在xml文件中,标签之间的字符串长度为51216,我将照片标签之间的十六进制数据读取为字符串并将其转换为字节[],然后使用FileOutputStream我正在写入一个文件。但是当我尝试打开图像文件时,它告诉我“文件似乎已损坏,损坏或文件太大”我尝试了很多方法来保存图像,但没有成功。我在下面列出了使用的方法。请帮助我摆脱这个困境。

String photo="0000005000000050FF191818FF15"; //this is just a sample.The photo String actually contains the full Hex String which is 51216 long
//METHOD 1
    String[] v = photo.split(" ");
    byte[] arr = new byte[v.length];
    int x = 0;
    for(String val: v) {
        arr[x++] =  Integer.decode("0x" + val).byteValue();

    }
     FileOutputStream fos=new FileOutputStream("D:/Images/image6.jpg");
    fos.write(arr);
    fos.flush();
    fos.close();


  //METHOD 2
    byte[] arr = new byte[photo.length()/2];
    for ( int start = 0; start < photo.length(); start += 2 )
    {
        String thisByte = photo.substring(start, start+2);
        arr[start/2] = Byte.parseByte(thisByte, 16);
    }
    FileOutputStream fos=new FileOutputStream("D:/Images/image6.jpg");
    fos.write(arr);
    fos.flush();
    fos.close();


  //METHOD 3
    if ((photo.length() % 2) != 0)
        throw new IllegalArgumentException("Input string must contain an even number of characters");

    final byte result[] = new byte[photo.length()/2];
    final char enc[] = photo.toCharArray();
    for (int x = 0; x < enc.length; x += 2) 
    {
        StringBuilder curr = new StringBuilder(2);
        curr.append(enc[x]).append(enc[x + 1]);
        result[x/2] = (byte) Integer.parseInt(curr.toString(), 16);
    }
    FileOutputStream fos=new FileOutputStream("D:/Images/image6.jpg");
    fos.write(result);
    fos.flush();
    fos.close();


  //METHOD 4
    byte result[] = new byte[photo.length()/2];
    char enc[] = photo.toUpperCase().toCharArray();
    StringBuffer curr;
    for (int x = 0; x < enc.length; x += 2) 
    {
        curr = new StringBuffer("");
        curr.append(String.valueOf(enc[x]));
        curr.append(String.valueOf(enc[x + 1]));
        result[x] = (byte) Integer.parseInt(curr.toString(), 16);
    }
    FileOutputStream fos=new FileOutputStream("D:/Images/image6.jpg");
    fos.write(result);
    fos.flush();
    fos.close();


  //METHOD 5
    int len = photo.length();
    byte[] data = new byte[len / 2];
    for (int x = 0; x < len; x += 2)
    {
        data[x / 2] = (byte) ((Character.digit(photo.charAt(x), 16) << 4)
                             + Character.digit(photo.charAt(x+1), 16));
    }
    FileOutputStream fos=new FileOutputStream("D:/Images/image6.jpg");
    fos.write(data);
    fos.flush();
    fos.close();


  //METHOD 6
    byte[] bytes=new BigInteger(photo, 16).toByteArray();
    FileOutputStream fos=new FileOutputStream("D:/Images/image6.jpg");
    fos.write(bytes);
    fos.flush();
    fos.close();


  //METHOD 7
    byte[] bytes =DatatypeConverter.parseHexBinary(photo);
    FileOutputStream fos=new FileOutputStream("D:/Images/image6.jpg");
    fos.write(bytes);
    fos.flush();
    fos.close();


  //METHOD 8
     HexBinaryAdapter adapter = new HexBinaryAdapter();
    byte[] bytes = adapter.unmarshal(photo);
    FileOutputStream fos=new FileOutputStream("D:/Images/image6.png");
    fos.write(bytes);
    fos.flush();
    fos.close();


  //METHOD 9
    byte data[] = new byte[photo.length()/2];
    for(int x=0;i < photo.length();x+=2) {
        data[x/2] = (Integer.decode("0x"+photo.charAt(x)+photo.charAt(x+1))).byteValue();
    }
    FileOutputStream fos=new FileOutputStream("D:/Images/image6.jpg");
    fos.write(data);
    fos.flush();
    fos.close();


  //METHOD 10
    byte[] data = new byte[photo.length()/2];
    for (int x=0;i<photo.length()/2;x++) 
    {
        data[x] = (Integer.decode(
                "0x"+photo.substring(x*2, (x+1)*2))).byteValue();
    }
    FileOutputStream fos=new FileOutputStream("D:/Images/image6.jpg");
    fos.write(data);
    fos.flush();
    fos.close();


  //METHOD 11
    String hexVal ="0000005000000050FF";
    //String hexVal = "0123456789ABCDEF";
    byte[] out = new byte[photo.length() / 2];
    int n = photo.length();
    for( int x = 0; x < n; x += 2 ) {
        int hn = hexVal.indexOf( photo.charAt( x ) );
        int ln = hexVal.indexOf( photo.charAt( x + 1 ) );

        out[x/2] = (byte)( ( hn << 4 ) | ln );
    }
    FileOutputStream fos=new FileOutputStream("D:/Images/image6.jpg");
    fos.write(out);
    fos.flush();
    fos.close();



    //METHOD 12
     byte[] array=photo.getBytes();
     FileOutputStream fos=new FileOutputStream("D:/Images/image6.jpg");
    fos.write(array);
    fos.flush();
    fos.close();


    //METHOD 13
    byte[] array=photo.getBytes();
    byte[] bytes = Base64.decode(array);
    FileOutputStream fos=new FileOutputStream("D:/Images/image6.jpg");
    fos.write(bytes);
    fos.flush();
    fos.close();


    //METHOD 14
    byte[] array=photo.getBytes();
    Charset csets = Charset.forName("UTF-8");
    ByteBuffer bb=ByteBuffer.wrap(array);
    csets.decode(bb);
    bb.rewind();
    byte[] array1=bb.array();
    FileOutputStream fos=new FileOutputStream("D:/Images/image6.jpg");
    fos.write(array1);
    fos.flush();
    fos.close();

最短路线可能是这样的。

String photo = "0000005000000050FF191818FF151715FF111413FF0E100FF2A2322FF292221";

// adds a dummy byte at the start to avoid truncation of leading zeros.
byte[] bytes = new BigInteger("10" + photo, 16).toByteArray();

System.out.println(Arrays.toString(bytes));

prints

[1, 0, 0, 0, 5, 0, 0, 0, 5, 15, -15, -111, -127, -113, -15, 81, 113, 95, -15, 17, 65, 63 , -16, -31, 0, -1, 42, 35, 34, -1, 41, 34, 33]

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将十六进制字符串转换为图像文件[重复] 的相关文章

随机推荐