简体   繁体   English

来自Databbase的Java bmp

[英]Java bmp from Databbase

I need to create a BMP (bitmap) image from a database using Java. 我需要使用Java从数据库创建BMP(位图)图像。 The problem is that I have huge sets of integers ranging from 10 to 100. 问题是我有大量的整数集,范围从10到100。

I would like to represent the whole database as a bmp. 我想将整个数据库表示为bmp。 The amount of data 10000x10000 per table (and growing) exceeds the amount of data I can handle with int arrays. 每个表(并不断增长)的10000x10000数据量超出了我可以使用int数组处理的数据量。

Is there a way to write the BMP directly to the hard drive, pixel by pixel, so I don't run out of memory? 有没有一种方法可以将BMP逐像素直接写入硬盘,所以我不会用光内存?

A file would work (I definitely woudln't do a per pixel call, you'll be waiting hours for the result). 一个文件可以工作(我绝对不会按像素调用,您将等待数小时才能得到结果)。 You just need a buffer. 您只需要一个缓冲区。 Break the application apart along the lines of -> 按照->将应用程序分开

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;
}

Read the documentation on your database driver, but any major database is going to optimize your ResultSet object so you can use a cursor and not worry about memory. 阅读数据库驱动程序上的文档,但是任何主要数据库都将优化ResultSet对象,以便您可以使用游标而不用担心内存。

All that being said... an int[10000][10000] isn't why you're running out of memory. 这么说... int [10000] [10000]并不是为什么内存不足。 Its probably what you're doing with those values and your algorithm. 您可能正在使用这些值和算法进行操作。 Example: 例:

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");
  }

}

Output -> 输出->

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

Edit--Or if I misinterpreted your question try this -> http://www.java2s.com/Code/Java/Database-SQL-JDBC/LoadimagefromDerbydatabase.htm 编辑-如果我误解了您的问题,请尝试-> http://www.java2s.com/Code/Java/Database-SQL-JDBC/LoadimagefromDerbydatabase.htm

I see... well take a look around, I'm rusty but this seems to be a way to do it. 我看到了……好好看看,我生锈了,但这似乎是一种方法。 I'd double check my buffering... 我会仔细检查我的缓冲...

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");
  }
}

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

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

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