0

我需要使用 Java 从数据库创建 BMP(位图)图像。问题是我有大量从 10 到 100 的整数。

我想将整个数据库表示为 bmp。每个表 10000x10000 的数据量(并且还在增长)超过了我可以使用 int 数组处理的数据量。

有没有办法将 BMP 直接逐个像素地写入硬盘,这样我就不会耗尽内存?

4

2 回答 2

1

一个文件可以工作(我绝对不会按像素调用,你会等待几个小时才能得到结果)。你只需要一个缓冲区。按照 -> 的方式拆分应用程序

int[] buffer = new int[BUFFER_SIZE];

ResultSet data = ....;  //Forward paging result set

while(true)
{
  for(int i = 0; i < BUFFER_SIZE; i++)
  {
    //Read result set into buffer

  }
  //write buffer to cache (HEAP/File whatever)

  if(resultSetDone)
    break;
}

阅读有关数据库驱动程序的文档,但任何主要数据库都会优化您的 ResultSet 对象,这样您就可以使用游标而不必担心内存。

话虽如此... int[10000][10000] 并不是您内存不足的原因。这可能是您对这些值和算法所做的事情。例子:

public class Test
{
  public static void main(String... args)
  {
    int[][] ints = new int[10000][];

    System.out.println(System.currentTimeMillis() + " Start");
    for(int i = 0; i < 10000; i++)
    {
      ints[i] = new int[10000];
      for(int j = 0; j < 10000; j++)
        ints[i][j] = i*j % Integer.MAX_VALUE / 2;

      System.out.print(i);
    }
    System.out.println();
    System.out.println(Integer.valueOf(ints[500][999]) + " <- value");

    System.out.println(System.currentTimeMillis() + " Stop");
  }

}

输出->

1344554718676 Start
//not even listing this
249750 <- value
1344554719322 Stop

编辑——或者如果我误解了你的问题,试试这个——> http://www.java2s.com/Code/Java/Database-SQL-JDBC/LoadimagefromDerbydatabase.htm

我明白了……看看周围,我生疏了,但这似乎是一种方法。我会仔细检查我的缓冲...

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class Test
{
  public static void main(String... args)
  {
    // 2 ^ 24 bytes, streams can be bigger, but this works...
    int size = Double.valueOf((Math.floor((Math.pow(2.0, 24.0))))).intValue();

    byte[] bytes = new byte[size];

    for(int i = 0; i < size; i++)
      bytes[i] = (byte) (i  % 255);

    ByteArrayInputStream stream = new ByteArrayInputStream(bytes); 

    File file = new File("test.io"); //kill the hard disk

    //Crappy error handling, you'd actually want to catch exceptions and recover
    BufferedInputStream in = new BufferedInputStream(stream);
    BufferedOutputStream out = null;
    byte[] buffer = new byte[1024 * 8];
    try
    {
      //You do need to check the buffer as it will have crap in it on the last read
      out = new BufferedOutputStream(new FileOutputStream(file));
      while(in.available() > 0)
      {
        int total = in.read(buffer);
        out.write(buffer, 0, total);
      }
    }
    catch (IOException e)
    {
      e.printStackTrace();
    }
    finally
    {
      if(out != null)
        try
        {
          out.flush();
          out.close();
        }
        catch (IOException e)
        {
          e.printStackTrace();
        }
    }

    System.out.println(System.currentTimeMillis() + " Start");

    System.out.println();
    System.out.println(Integer.valueOf(bytes[bytes.length - 1]) + " <- value");
    System.out.println("File size is-> " + file.length());
    System.out.println(System.currentTimeMillis() + " Stop");
  }
}
于 2012-08-09T23:27:28.223 回答
0

您可以将其保存为文件,从概念上讲,它只是一个字节序列。

于 2012-08-09T23:06:53.390 回答