Java中的base64编解码和字节数组的读写

=Start=

缘由:

杂项知识的记录,比如最小的png/gif图片大小是多少,以及通过字节数组快速生成图片还有Java中如何进行base64编解码等内容。

正文:

参考解答:

最小的透明PNG 是 68 bytes,如果不要求透明还可以减少 1 byte只有 67 bytes;
最小的GIF是 35 bytes。

其它的直接看代码:

package com.example;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Base64;

/**
 * @author ixyzero
 * Created on 2022-05-10
 */
public class opMisc {
    public static void main(String[] args) {
        // 字节数组的声明和打印
        byte[] pngBytes = {-119, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 0, 1, 0, 0, 0, 1,
                8, 4, 0, 0, 0, -75, 28, 12, 2, 0, 0, 0, 11, 73, 68, 65, 84, 120, -38, 99, 100, 96, 0, 0, 0, 6, 0,
                2, 48, -127, -48, 47, 0, 0, 0, 0, 73, 69, 78, 68, -82, 66, 96, -126};
        System.out.println(Arrays.toString(pngBytes));

        String b64encStr = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=";
        // base64字符串的解码
        byte[] decoded = Base64.getDecoder().decode(b64encStr);
        System.out.println(Arrays.toString(decoded)); // print byte array

        // hex print byte array
        System.out.println(bytesToHex(decoded));

        // 将字节数组写入文件
        try {
            Files.write(Paths.get("test-output-bytes.png"), decoded); // filetype: PNG image data (68 bytes)
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 将文件内容读入字节数组
        try {
            byte[] fileData = Files.readAllBytes(Paths.get("test-output-bytes.png"));
            System.out.println(Arrays.toString(fileData));
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 将字节数组转换成字符串(直接new String就可以)
        String decodedStr = new String(decoded);
        System.out.println(decodedStr);
        String decodedStr2 = b64DecodeString(b64encStr);
        System.out.println(decodedStr2.equals(decodedStr)); // true

        // 将字符串写入文件
        try( BufferedWriter writer = new BufferedWriter(new FileWriter("test-output-str.png")) ) {
            writer.write(decodedStr); // filetype: data (82 bytes)
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 将文件内容读入字节数组
        try {
            byte[] fileData = Files.readAllBytes(Paths.get("test-output-str.png"));
            System.out.println(Arrays.toString(fileData));
        } catch (IOException e) {
            e.printStackTrace();
        }

        String raw = "hello world";
        System.out.println(b64EncodeString(raw));
        System.out.println(b64DecodeString(b64EncodeString(raw)));
    }

    public static String b64EncodeString(String plainString) {
        return  Base64.getEncoder().encodeToString(plainString.getBytes());
    }

    public static String b64DecodeString(String encodedString) {
        byte[] bytes = Base64.getDecoder().decode(encodedString);
        return new String(bytes);
    }

    private static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();
    public static String bytesToHex(byte[] bytes) {
        char[] hexChars = new char[bytes.length * 2];
        for (int j = 0; j < bytes.length; j++) {
            int v = bytes[j] & 0xFF;
            hexChars[j * 2] = HEX_ARRAY[v >>> 4];
            hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F];
        }
        return new String(hexChars);
    }

}

参考链接:

Smallest possible transparent PNG
https://garethrees.org/2007/11/14/pngcrush/

TRANSPARENT BASE64 PNG PIXEL GENERATOR #68 bytes(Smallest 1×1 transparent PNG pixel possible)
https://png-pixel.com/

smallest filesize for transparent single pixel image
https://stackoverflow.com/questions/2570633/smallest-filesize-for-transparent-single-pixel-image

The smallest transparent pixel
http://proger.i-forge.net/%D0%9A%D0%BE%D0%BC%D0%BF%D1%8C%D1%8E%D1%82%D0%B5%D1%80/[20121112]%20The%20smallest%20transparent%20pixel.html

Portable Network Graphics (PNG) Specification (Second Edition)
https://www.w3.org/TR/PNG/

Java中如何将图片转换成字节数组 – how to convert image to byte array in java? [duplicate]
https://stackoverflow.com/questions/3211156/how-to-convert-image-to-byte-array-in-java

Java中如何打印字节数组 – How to print the data in byte array as characters?
https://stackoverflow.com/questions/6463580/how-to-print-the-data-in-byte-array-as-characters

Java中如何将字节数组存为图片文件 – How to store a byte array as an image file on disk?
https://stackoverflow.com/questions/2138913/how-to-store-a-byte-array-as-an-image-file-on-disk

=END=

声明: 除非注明,ixyzero.com文章均为原创,转载请以链接形式标明本文地址,谢谢!
https://ixyzero.com/blog/archives/5255.html

发表评论

您的电子邮箱地址不会被公开。