は、ここでは「深いを作成するための3つの方法があります「コピーサブイメージ:
// Create an image
BufferedImage image = new BufferedImage(100, 100, BufferedImage.TYPE_4BYTE_ABGR);
// Fill with static
new Random().nextBytes(((DataBufferByte) image.getRaster().getDataBuffer()).getData());
あなたがgetData(rect)
から取得Raster
のすでに深いコピー周辺の画像を作成します。これにはWritableRaster
へのキャストが含まれているため、一部のJava実装または将来的には破損する可能性があります。
// Get sub-raster, cast to writable and translate it to 0,0
WritableRaster data = ((WritableRaster) image.getData(new Rectangle(25, 25, 50, 50))).createWritableTranslatedChild(0, 0);
// Create new image with data
BufferedImage subOne = new BufferedImage(image.getColorModel(), data, image.isAlphaPremultiplied(), null);
を別のオプション、「通常の方法」のサブ画像を作成し、その新しいイメージへのラスタのコピー:あなたは一度だけデータをコピーして、非常に高速である必要があります。
// Get subimage "normal way"
BufferedImage subimage = image.getSubimage(25, 25, 50, 50);
// Create empty compatible image
BufferedImage subTwo = new BufferedImage(image.getColorModel(), image.getRaster().createCompatibleWritableRaster(50, 50), image.isAlphaPremultiplied(), null);
// Copy data into the new, empty image
subimage.copyData(subTwo.getRaster());
最後に、簡単にルート、ちょうど新しい空の画像の上にサブイメージを描く:まだ、一度だけコピー(なし鋳物を)一つのサブラスタを作成する必要があります。レンダリングのパイプラインが含まれているため、やや遅くなる可能性がありますが、それはまだ合理的に実行する必要があります。
// Get subimage "normal way"
BufferedImage subimage = image.getSubimage(25, 25, 50, 50);
// Create new empty image of same type
BufferedImage subThree = new BufferedImage(50, 50, image.getType());
// Draw the subimage onto the new, empty copy
Graphics2D g = subThree.createGraphics();
try {
g.drawImage(subimage, 0, 0, null);
}
finally {
g.dispose();
}
サブ画像を抽出し、それは別のBufferedImageを行うペイント、あなたの心のコンテンツ – MadProgrammer
に変更し、それがcomputationnaly効率的ですか? –
私は分かりません。シンプルで機能していて、サブイメージのコピーですが、変更されたときに元のイメージには反映されないBufferedImageを提供します。行って、あなたのために何が効果があるかを確認するためにあなた自身の比較のいくつかをしてください – MadProgrammer