2010-12-18 10 views
3

特定の解像度のRGBピクセル値をJavaに読み込むことができる必要があります。その解像度は小さいです(〜300x300)。サムネイルピクセル値をJavaにロードする最速の方法

現在、私はこのようにそれらをロードしてきた:

File file = new File("...path...");  
BufferedImage imsrc = ImageIO.read(file); 
int width = imsrc.getWidth(); 
int height = imsrc.getHeight();  
int[] data = new int[width * height];  
imsrc.getRGB(0,0, width, height, data, 0, width); 

し、それを自分自身を小型化。

サムはので、ここでそれは、ダウンサイジングコードを尋ね:

/** 
* DownSize an image. 
* This is NOT precise, and is noisy. 
* However, this is fast and better than NearestNeighbor 
* @param pixels - _RGB pixel values for the original image 
* @param width - width of the original image 
* @param newWidth - width of the new image 
* @param newHeight - height of the new image 
* @return - _RGB pixel values of the resized image 
*/ 
public static int[] downSize(int[] pixels, int width, int newWidth, int newHeight) { 
    int height = pixels.length/width; 
    if (newWidth == width && height == newHeight) return pixels; 
    int[] resized = new int[newWidth * newHeight]; 
    float x_ratio = (float) width/newWidth; 
    float y_ratio = (float) height/newHeight; 
    float xhr = x_ratio/2; 
    float yhr = y_ratio/2; 
    int i, j, k, l, m; 
    for (int x = 0; x < newWidth; x ++) 
     for (int y = 0; y < newHeight; y ++) {    
      i = (int) (x * x_ratio); 
      j = (int) (y * y_ratio); 
      k = (int) (x * x_ratio + xhr); 
      l = (int) (y * y_ratio + yhr); 
      for (int p = 0; p < 3; p ++) { 
       m = 0xFF << (p * 8); 
       resized[x + y * newWidth] |= (
         (pixels[i + j * width] & m) + 
         (pixels[k + j * width] & m) + 
         (pixels[i + l * width] & m) + 
         (pixels[k + l * width] & m) >> 2) & m; 
      } 
     } 
    return resized; 
} 

最近、私はImageMagickのの「変換」で、私はダウンサイズできることを実現し、その方法はダウンサイズバージョンをロード。これによりさらに33%の節約になります。

もっと良い方法があれば、私は不思議に思っていました。

EDIT:私のコードが一般的に良いのかどうか疑問に思う人もいますが、答えはNOです。すでに小さな画像(640x480、そうでなければ.getRGB()は永遠に使用されています)をダウンサイズしているので、私が使用したコードはうまく動作します。私は一部の人々が本当に気にしていることを知っています。

+0

データ配列とGraphics.drawImageの両方を使用して、サイズ変更コードを投稿することができますか? –

答えて

3

はここで最適な方法でJavaでサムネイルを生成する上で非常に良い記事です:

http://today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html

あなたは異なるスケーリング/レンダリングパラメータを指定すると、より良い結果を有することができます。

Graphics2D g2 = (Graphics2D)g; 
    int newW = (int)(originalImage.getWidth() * xScaleFactor); 
    int newH = (int)(originalImage.getHeight() * yScaleFactor); 
    g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, 
         RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR); 
    g2.drawImage(originalImage, 0, 0, newW, newH, null); 
関連する問題