代码之家  ›  专栏  ›  技术社区  ›  Errorfreak

如何使用Visual C#和LibJpeg.Net从Jpeg获取DCT系数

  •  0
  • Errorfreak  · 技术社区  · 11 年前

    我需要在量化后得到DCT系数数组,以便进一步改变比特(隐写术)。 我的问题是:比方说,我在picturebox或其他任何地方有jpeg图像。我怎样才能获得dct系数。使用C#和像LibJpeg.Net这样的库?需要一个代码。在整个网站上找不到任何完整和简单的内容。此外,在LibJpeg.Net上看不到任何教程。

    在此步骤之后:

        BitMiracle.LibJpeg.Classic.jpeg_decompress_struct oJpegDecompress = new BitMiracle.LibJpeg.Classic.jpeg_decompress_struct();
         System.IO.FileStream oFileStreamImage = new System.IO.FileStream(strImagePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
           oJpegDecompress.jpeg_stdio_src(oFileStreamImage);
            oJpegDecompress.jpeg_read_header(true);            
            BitMiracle.LibJpeg.Classic.jvirt_array<BitMiracle.LibJpeg.Classic.JBLOCK>[] JBlock = oJpegDecompress.jpeg_read_coefficients();
    

    我现在该怎么做,编辑dct系数?使用 .Access() ? 我如何使用这个?有什么例子吗?

    以下内容:

    short[] block = JBlock[c].Access(x, y);
    

    给出如下错误:“无法将类型'BitMiracle.LibJpeg.Classic.JBLOCK[][]'隐式转换为'short[]'”

    此外,当使用类似的东西时,它会给出一个关于将“BitMiracle.LibJpeg.Classic.JBLOCK[]”转换为“System.IConvertable”的错误。

    或者也许有人知道解决我问题的另一个简单方法?

    2 回复  |  直到 11 年前
        1
  •  2
  •   Community Mohan Dere    9 年前

    好吧,我想通了。至少,它回答了我的主要问题。

    private void button1_Click(object sender, EventArgs e)
            {
                string path = @"D:\067.jpg";
                var img = new Bitmap(path);
                var jo = img.Width; 
                var joj = img.Height;
                BitMiracle.LibJpeg.Classic.jpeg_decompress_struct oJpegDecompress = new BitMiracle.LibJpeg.Classic.jpeg_decompress_struct();
                System.IO.FileStream oFileStreamImage = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read);
                oJpegDecompress.jpeg_stdio_src(oFileStreamImage);
                oJpegDecompress.jpeg_read_header(true);
                BitMiracle.LibJpeg.Classic.jvirt_array<BitMiracle.LibJpeg.Classic.JBLOCK>[] JBlock = oJpegDecompress.jpeg_read_coefficients();
                var ll = JBlock[0].Access(0, 1); // accessing the element
                var oo = 5; // its gonna be new value for coefficient
                for (int i = 0; i < 64; i++) // some cycle
                {
                    ll[0][i][0] = Convert.ToInt16(oo); // changes
                }
                oJpegDecompress.jpeg_finish_decompress(); 
                oFileStreamImage.Close();
                /////
                System.IO.FileStream objFileStreamMegaMap = System.IO.File.Create(@"D:\068.jpg");
                BitMiracle.LibJpeg.Classic.jpeg_compress_struct oJpegCompress = new BitMiracle.LibJpeg.Classic.jpeg_compress_struct();
                oJpegCompress.jpeg_stdio_dest(objFileStreamMegaMap);
                oJpegDecompress.jpeg_copy_critical_parameters(oJpegCompress);
                oJpegCompress.Image_height = joj;
                oJpegCompress.Image_width = jo;
                oJpegCompress.jpeg_write_coefficients(JBlock);          
                oJpegCompress.jpeg_finish_compress();
                objFileStreamMegaMap.Close();
                oJpegDecompress.jpeg_abort_decompress();
                oFileStreamImage.Close();
            }
    

    有点马虎,但还是,只是测试。。。 使用了来自 here

    就像这样,正如您在控制台中看到的那样,0下的每个第0个元素->m_buffer->0->输出图像中的i将等于5

    向我欢呼。

        2
  •  -1
  •   user3344003    11 年前

    我在编写一个JPEG库以验证其正确性时已经完成了此操作。您只需将源代码转换为LIBJPEG即可;确定它在哪里执行您感兴趣的函数int(因为代码复杂,所以有些困难);设置断点或返回。