6

我有用于嵌入式系统的 C 语言源代码,其中包含每像素 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 或 C++ 编写自定义应用程序。

谢谢你的建议。

4

3 回答 3

2

使用 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);
于 2011-08-16T17:05:19.297 回答
1

我可以回答问题 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);

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

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

注意:我不是 100% 确定创建 Bitmap 对象时使用的 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;
于 2011-08-16T15:55:19.933 回答
0

您可以在 java 中使用 ImageIO 类。

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);
于 2011-08-16T15:50:30.950 回答