来自数据库的Java bmp

时间:2012-08-09 23:04:44

标签: java database bmp

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

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

有没有办法将BMP直接写入硬盘驱动器,因此我不会耗尽内存?

2 个答案:

答案 0 :(得分: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");
  }

}

输出 - &gt;

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

编辑 - 或者如果我误解了您的问题,请尝试以下操作 - &gt; 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");
  }
}

答案 1 :(得分:0)

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