java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Java视频压缩和解压

Java实现视频初步压缩和解压的代码示例

作者:Ha_Ha_Wu

从摄像头读取每一帧的图片,用一些简单的方法将多张图片信息压缩到一份文件中(自定义的视频文件),自定义解码器读取视频文件,并将每帧图片展示成视频,本文主要介绍了Java实现视频初步压缩和解压,需要的朋友可以参考下

第一步:按照某些算法帧内压缩

常见的视频压缩算法(H264,H265,MP4)过程很复杂,实现的压缩比率也很恐怖(H265可以做到0.5%的压缩率,也就是就算每帧图片加起来有2个GB,合并起来的视频也就10MB),其中压缩算法流程大致如下,我的程序没有细究算法,简单实现了25%的压缩率。

帧内压缩:

帧间压缩:

我的代码:

@GetMapping("/compressedVideos")
public void getCompressedBytes() throws IOException {
	//录制5秒的视频,存在List中
    webcam.open();
    long startTime = System.currentTimeMillis();
    List<BufferedImage> bufferedImages = new ArrayList<>();
    while (System.currentTimeMillis() - startTime < 5000) {
        BufferedImage image = webcam.getImage();
        bufferedImages.add(image);
    }
    System.out.println("录制结束");
    webcam.close();
    //调用压缩方法,将结果写入文件中
    byte[] bytes = outerCompressionUtils.photosToCompressedBytes(bufferedImages);
    File file = new File("压缩中的压缩.dat");
    FileOutputStream fos = new FileOutputStream(file);
    fos.write(bytes);
    fos.close();
    System.out.println("持久化结束");
}

压缩:

public static int[] rgb2YUV(int rgb) {
    int[] rgb1 = photoOps.RGBToInts(rgb);
    int red = rgb1[0];
    int green = rgb1[1];
    int blue = rgb1[2];
    int Y = (int) (0.299 * red + 0.587 * green + 0.114 * blue -128); //-128 到 127
    int U = (int) (-0.1684 * red - 0.3316 * green + 0.5 * blue);//-128 到 127
    int V = (int) (0.5 * red - 0.4187 * green - 0.083 * blue); //-128 到 127
    return new int[]{Y, U, V};
}
public static byte[] compressToOneChannel(BufferedImage bufferedImage) {
    byte[] Ys = new byte[bufferedImage.getWidth() * bufferedImage.getHeight()];
    byte[] Us = new byte[bufferedImage.getHeight() * (bufferedImage.getWidth() / 2)];
    byte[] Vs = new byte[(bufferedImage.getWidth() / 2) * (bufferedImage.getHeight() / 2)];
    int targetYs = 0;
    int targetUs = 0;
    int targetVs = 0;
	/*
	这里就是遍历2*2的宏块,将其中对应YUV分别写到YUV的数组中
	需要注意的是我犯的一个错误:没有注意到Y和U的遍历过程,导致在解码的时候图片异常
	*/
    for (int i = 0; i < bufferedImage.getHeight(); i += 2) {
        for (int j = 0; j < bufferedImage.getWidth(); j += 2) {
            for (int k = 0; k < 2; k++) {
                for (int l = 0; l < 2; l++) {
                    int[] ints = rgb2YUV(bufferedImage.getRGB(j + l, i + k));
                    int Y = ints[0];
                    Ys[targetYs] = (byte) (Y);
                    targetYs++;
                }
                int[] ints = rgb2YUV(bufferedImage.getRGB(j, i + k));
                int U = ints[1];
                Us[targetUs] = (byte) (U);
                targetUs++;
            }
            int[] ints = rgb2YUV(bufferedImage.getRGB(j, i));
            int V = ints[2];
            Vs[targetVs] = (byte) (V);
            targetVs++;
        }
    }
    int length1 = Ys.length; //大小估计 : 图片3000*2000 = 6000000 不会超int范围
    int length2 = Us.length;
    int length3 = Vs.length;
    byte[] targetBytes = new byte[4 * 5 + length1 + length2 + length3];
    int targetIndex = 0;
	//这里是将byte[]开头填充一些用于解码的信息,因为Ys,Us,Vs都是一起传的,需要在包开头标明每个数组长度
	//Y区的长度
    byte[] bytes1 = intToByte(length1);
    for (byte b : bytes1) {
        targetBytes[targetIndex] = b;
        targetIndex++;
    }
    //U区长度
    byte[] bytes2 = intToByte(length2);
    for (byte b : bytes2) {
        targetBytes[targetIndex] = b;
        targetIndex++;
    }
    //V区长度
    byte[] bytes3 = intToByte(length3);
    for (byte b : bytes3) {
        targetBytes[targetIndex] = b;
        targetIndex++;
    }
    //图片的高
    byte[] bytes4 = intToByte(bufferedImage.getHeight());
    for (byte b : bytes4) {
        targetBytes[targetIndex] = b;
        targetIndex++;
    }
    //图片的宽
    byte[] bytes5 = intToByte(bufferedImage.getWidth());
    for (byte b : bytes5) {
        targetBytes[targetIndex] = b;
        targetIndex++;
    }
	//传递真实数据
    for (byte y : Ys) {
        targetBytes[targetIndex] = y;
        targetIndex++;
    }
    for (byte u : Us) {
        targetBytes[targetIndex] = u;
        targetIndex++;
    }
    for (byte v : Vs) {
        targetBytes[targetIndex] = v;
        targetIndex++;
    }
    return targetBytes;
}
public static byte[] photosToCompressedBytes(List<BufferedImage> bufferedImages) throws IOException {
    //数据流中未必要有各种辅助信息,比如各类字段长度,在外规定好算了
    //这里每一帧的长度就是:20 + 640 * 480 * 1.75
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    //java提供的压缩工具,此输出流将输出的东西压缩输出
    //传入的Deflater对象用于控制压缩算法
    DeflaterOutputStream dos = new DeflaterOutputStream(baos,new Deflater());
	//帧信息添加到压缩流
    for (BufferedImage bufferedImage: bufferedImages
         ) {
        byte[] bytes = innerCompressionUtils.compressToOneChannel(bufferedImage);
        System.out.println("一帧的长度为:"+bytes.length);
        dos.write(bytes);
    }
    byte[] compressedData = baos.toByteArray();
    return compressedData;
}
class HuffmanNode implements Comparable<HuffmanNode>{
    byte value;
    int frequency;
    HuffmanNode left;
    HuffmanNode right;
    public HuffmanNode(byte value,int frequency){
        this.value = value;
        this.frequency = frequency;
    }
    @Override
    public int compareTo(@NotNull HuffmanNode o) {
        return this.frequency - o.frequency;
    }
}
public class Huffman {
    public static Map<Byte,String> encodingTable;
    public static String huffmanEncoding(byte[] originalBytes){
        Map<Byte,Integer> frequencyMap = new HashMap<>();
        for (byte b: originalBytes
             ) {
            frequencyMap.put(b, frequencyMap.getOrDefault(b,0)+1);
        }
        PriorityQueue<HuffmanNode> minHeap = new PriorityQueue<>();
        for (Map.Entry<Byte, Integer> entry : frequencyMap.entrySet()
                ) {
            minHeap.add(new HuffmanNode(entry.getKey(),entry.getValue()));
        }
        while (minHeap.size()>1){
            HuffmanNode left = minHeap.poll();
            HuffmanNode right = minHeap.poll();
            HuffmanNode mergeNode = new HuffmanNode((byte)0, left.frequency + right.frequency);
            mergeNode.left = left;
            mergeNode.right = right;
            minHeap.add(mergeNode);
        }
        encodingTable = new HashMap<>();
        HuffmanNode root = minHeap.poll();
        buildEncodingTable(root,"",encodingTable);
        StringBuilder encodingData = new StringBuilder();
        for (Byte b: originalBytes
             ) {
            encodingData.append(encodingTable.get(b));
        }
        System.out.println("原始数组长度"+originalBytes.length);
        System.out.println("哈夫曼后数组长度"+encodingData.length());
        return encodingData.toString();
    }
public static void buildEncodingTable(HuffmanNode node,String currentCode,Map<Byte,String> encodingMap) {
        if (node == null) {
            return;
        }
        if (node.left == null && node.right == null) {
            encodingMap.put(node.value, currentCode);
        } else {
            buildEncodingTable(node.left, currentCode + "0", encodingMap);
            buildEncodingTable(node.right, currentCode + "1", encodingMap);
        }
    }

但其实这里用哈夫曼并不会优化数据量,原因如下: 我传输的数据是-128到127的byte类型,这些byte来自图片的亮度和色度,调试中发现这255个数字出现的频率差不多,全部都在14万到20万之间,两个最小值加起来任然比最大值大,这就意味着这颗哈夫曼树会比较满,类似完全二叉树,于是就无法区分出现频率最高的某个字符。

另外,原本255个数将8位byte全都占满,假如有一个频率很高的元素,我们把较短的0101赋给它,那势必会导致原本以0101开头的元素用8位以上的长度进行表示,而程序中各元素出现频率相近,这就会导致如果有元素用短于8位的编码,其他长于8位编码的元素会导致数据更加庞大。

我在用huffman编码后,数据量一点都没有变,只是由长度为40647865的byte数组变成长度为325182920的字符串,其实就是×8 。怀疑是代码哪里错了...

常见的压缩算法是将DCT变换后的结果进行哈夫曼编码,DCT变换后低频信息和高频信息自然区分开,确实更适合这个熵编码方法

public static InflaterInputStream inflaterCompressedBytes(byte[] bytes) throws IOException {
        //解压数据
        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
        InflaterInputStream lis = new InflaterInputStream(bais, new Inflater());
        return lis;
    }
public static BufferedImage getBfi(byte[] originalBytes) {
		//分别先把开头表示各个区长度以及图片宽高的参数取出来
        byte one = originalBytes[0];
        byte two = originalBytes[1];
        byte three = originalBytes[2];
        byte four = originalBytes[3];
        int Y = ((one & 0xff) << 24) | ((two & 0xff) << 16) | ((three & 0xff) << 8) | (four & 0xff);
        byte one2 = originalBytes[4];
        byte two2 = originalBytes[5];
        byte three2 = originalBytes[6];
        byte four2 = originalBytes[7];
        int U = ((one2 & 0xff) << 24) | ((two2 & 0xff) << 16) | ((three2 & 0xff) << 8) | (four2 & 0xff);
        byte one3 = originalBytes[8];
        byte two3 = originalBytes[9];
        byte three3 = originalBytes[10];
        byte four3 = originalBytes[11];
        int V = ((one3 & 0xff) << 24) | ((two3 & 0xff) << 16) | ((three3 & 0xff) << 8) | (four3 & 0xff);
        byte one4 = originalBytes[12];
        byte two4 = originalBytes[13];
        byte three4 = originalBytes[14];
        byte four4 = originalBytes[15];
        int height = ((one4 & 0xff) << 24) | ((two4 & 0xff) << 16) | ((three4 & 0xff) << 8) | (four4 & 0xff);
        byte one5 = originalBytes[16];
        byte two5 = originalBytes[17];
        byte three5 = originalBytes[18];
        byte four5 = originalBytes[19];
        int width = ((one5 & 0xff) << 24) | ((two5 & 0xff) << 16) | ((three5 & 0xff) << 8) | (four5 & 0xff);
        System.out.println("Y: " + Y);
		//将数据读取出来
        byte[] Ys = Arrays.copyOfRange(originalBytes, 20, Y + 20);
        byte[] Us = Arrays.copyOfRange(originalBytes, Y + 20, Y + U + 20);
        byte[] Vs = Arrays.copyOfRange(originalBytes, Y + U + 20, Y + U + V + 20);
        BufferedImage bfi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        int hongW = width / 2;
        int hongH = height / 2;
		//用YUV数据恢复成RGB,填充到图片的每一个像素
        for (int i = 0; i < height - 1; i++) {
            for (int j = 0; j < width - 1; j++) {
                int H = i / 2;
                int W = j / 2;
                byte y = Ys[(i / 2 * 2) * width + j / 2 * 4 + (i % 2) * 2 + j % 2];
                byte u = Us[H * hongW * 2 + j / 2 * 2 + i % 2];
                byte v = Vs[H * hongW + W];
                int r = (int) (y + 128 + 1.14075 * (v));
                int g = (int) (y + 128 - 0.3455 * (u) - 0.7169 * (v));
                int b = (int) (y + 128 + 1.779 * (u));
                r = Math.min(255, Math.max(0, r));
                g = Math.min(255, Math.max(0, g));
                b = Math.min(255, Math.max(0, b));
                int color = (r) << 16 | (g) << 8 | b;
                if (i < 1 && j < 20) {
                bfi.setRGB(j, i, color);
            }
        }
        return bfi;
    }
FileInputStream fileInputStream = new FileInputStream("C:\\Users\\吴松林\\IdeaProjects\\meitu2\\压缩中的压缩.dat");
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); //此输出流中写入所有信息,最后转出为byte[],类似桶子
        byte[] buffer = new byte[1024];
        int bytesRead;
        while ((bytesRead = fileInputStream.read(buffer))!=-1){
            outputStream.write(buffer,0,bytesRead);
        }
        byte[] data = outputStream.toByteArray();
        InflaterInputStream iutputStream1 = utils.inflaterCompressedBytes(data); //解压
        BufferedInputStream bis = new BufferedInputStream(iutputStream1);
        List<BufferedImage> bufferedImages = new ArrayList<>();
        byte[] eachImage = new byte[(int) (20+640*480*1.75)];
        int testIndex = 0;
        int index;
        System.out.println("length: "+eachImage.length);
        try {
            while ((index = bis.read(eachImage)) != -1) {
                System.out.println("本次读取长度:" + index);
                testIndex++;
                System.out.println("test: " + testIndex);
                BufferedImage bfi = utils.getBfi(eachImage);
                bufferedImages.add(bfi);
            }
        }catch (Exception e){
            System.out.println("跳过异常,省略最后一张图片");
            e.printStackTrace();
        }
        bis.close();
        iutputStream1.close();
        outputStream.close();
        fileInputStream.close();
        JFrame jFrame = new JFrame();
        myPanel panel = new myPanel();
        jFrame.add(panel);
        jFrame.setSize(new Dimension(640,480));
        jFrame.setVisible(true);
        panel.list = bufferedImages;
        while (true){
            panel.repaint();
        }
    }
}
class myPanel extends JPanel{
    int index = 0;
    List<BufferedImage> list;
    @Override
    public void paint(Graphics g) {
        g.drawImage(list.get(index), 0, 0, null);
        if (index < list.size() - 2) {
            index++;
        }
        try {
            Thread.sleep(34);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}

注意:

以上就是Java实现视频初步压缩和解压的代码示例的详细内容,更多关于Java视频压缩和解压的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:
阅读全文