繁体   English   中英

将原始灰度二进制转换为 JPEG

[英]Convert raw grayscale binary to JPEG

我有 C 语言源代码,用于嵌入式系统,包含 arrays 数据,用于每像素 8 位灰度图像。 我负责记录软件,我想将此源代码转换为 JPEG(图像)文件。

这是一个代码示例:

const unsigned char grayscale_image[] = {
0, 0, 0, 0, 0, 0, 0, 74, 106, 159, 159, 159, 159, 159, 159, 159, 159, 159, 159, 159, 159, 159, 
159, 159, 159, 159, 159, 159, 159, 159, 159, 159, 146, 93, 39, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
//...
};
const unsigned int height = 41;
const unsigned int width = 20;

这是我的问题:(是的,复数)

  1. 您推荐哪些应用程序将此源文件转换为 JPEG?
  2. GIMP 或 Paint 可以导入 CSV 数据文件吗?
  3. 如果我编写这个自定义应用程序,JPEG 存在哪些 Java 库?
  4. C# 中存在哪些库来完成此任务?

我可以使用以下资源:MS Visio 2010、Gimp、Paint、Java、Eclipse、MS Visual Studio 2010 Professional、wxWidgets、wxFrameBuilder、Cygwin。
我可以在 C#、Java、C 或 ZF6F87C9FDCF1B3C3F097F923C 中编写自定义应用程序。

谢谢你的建议。

使用 java 的问题是将字节获取为整数。 在读入时,您需要转换为 int 以捕获大于 127 的值,因为 java 没有无符号字节。

int height=41;
int width=20;
int[] data = {...};

BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
for ( int x = 0; x < width; x++ ) {
  for ( int y = 0; y < height; y++ ) {
  // fix this based on your rastering order
  final int c = data[ y * width + x ];
  // all of the components set to the same will be gray
  bi.setRGB(x,y,new Color(c,c,c).getRGB() );
  }
}
File out = new File("image.jpg");
ImageIO.write(bi, "jpg", out);

我可以回答问题 4,我可以在 c# 中为您提供执行此操作的代码。 这很简单...

int width = 20, height = 41;
byte[] grayscale_image = {0, 0, 0, ...};
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(width, height);
int x = 0;
int y = 0;
foreach (int i in grayscale_image)
{
    bitmap.SetPixel(x, y, System.Drawing.Color.FromArgb(i, i, i));
    x++;
    if (x >= 41)
    {
        x = 0;
        y++;
    }
}
bitmap.Save("output.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

如果您四处寻找 bitmap 优化技术(例如锁定 bitmap 内存),您也可以优化此代码

编辑:位锁定的替代方案(应该更快)......

注意:我不是 100% 确定创建 Bitmap object 时使用的 PixelFormat - 是我对可用选项的最佳猜测。

int width = 20, height = 41;
byte[] grayscale_image = {0, 0, 0, ...};
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(width, height, PixelFormat.Format8bppIndexed);

System.Drawing.Imaging.BitmapData bmpData = bitmap.LockBits(
                     new Rectangle(0, 0, bitmap.Width, bitmap.Height),
                     ImageLockMode.WriteOnly, bitmap.PixelFormat);

System.Runtime.InteropServices.Marshal.Copy(bytes, 0, bmpData.Scan0, bytes.Length);

bitmap.UnlockBits(bmpData);

return bitmap;

您可以只使用 java 中的 ImageIO class。

BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GREY);

Graphics2D g2 = bi.createGraphics();

//loop through and draw the pixels here   

File out = new File("Myimage.jpg");
ImageIO.write(bi, "jpg", out);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM