Java で写真を結合する方法

この記事では、Java で photos をマージする方法 について説明します。 Java でフォト ジョイナーを作成するための環境の準備、段階的な手順、および実行可能なサンプル コードについて説明します。この情報は、さまざまなプラットフォーム上の Java 環境でこの機能を使用する場合に役立ちます。

Java で写真を結合する手順

  1. リポジトリ マネージャーで Aspose.Imaging for Java を使用するための環境を準備する
  2. 出力画像のサイズを計算するための画像のリストを作成します。
  3. ソース画像を 1 つに結合して出力ソースを作成します
  4. JpegOptions クラスのさまざまなプロパティを指定します
  5. JpegImage クラス インスタンスを使用して結合された画像をレンダリングします

上記の手順では、Java で写真を結合する完全なプロセスについて詳しく説明します。最初のステップでは、1 つの写真に結合するさまざまな画像をリストします。次に、新しい写真を作成し、要件に基づいてディスクまたはストリームに書き込む前に、それを出力イメージにエクスポートします。

Java で写真を結合するコード

import com.aspose.imaging.*;
public class Main {
public static void main(String[] args) throws Exception // Merge photos in Java
{
// Set the licenses
new License().setLicense("License.lic");
// Creating an array of strings
String[] imagePaths = new String[3];
// Initializing array elements
imagePaths[0] = "Sample1.jpg";
imagePaths[1] = "Sample2.jpg";
imagePaths[2] = "Sample3.jpg";
// Path of output image
String outputPath = "output-combine.jpg";
// Get resulting image size
int newWidth = 0;
int newHeight = 0;
for (String imagePath : imagePaths) {
try (com.aspose.imaging.RasterImage image = (com.aspose.imaging.RasterImage) com.aspose.imaging.Image.load(imagePath)) {
com.aspose.imaging.Size size = image.getSize();
newWidth = Math.max(newWidth, size.getWidth());
newHeight += size.getHeight();
}
}
// Combine images into new one
try (com.aspose.imaging.imageoptions.JpegOptions options = new com.aspose.imaging.imageoptions.JpegOptions()) {
options.setSource(new com.aspose.imaging.sources.StreamSource()); // empty
options.setQuality(100);
// Create resultant image
try (com.aspose.imaging.fileformats.jpeg.JpegImage newImage = (com.aspose.imaging.fileformats.jpeg.JpegImage) com.aspose.imaging.Image.create(options, newWidth, newHeight)) {
int stitchedHeight = 0;
for (String imagePath : imagePaths) {
try (com.aspose.imaging.RasterImage image = (com.aspose.imaging.RasterImage) com.aspose.imaging.Image.load(imagePath)) {
com.aspose.imaging.Rectangle bounds = new com.aspose.imaging.Rectangle(0, stitchedHeight, image.getWidth(), image.getHeight());
newImage.saveArgb32Pixels(bounds, image.loadArgb32Pixels(image.getBounds()));
stitchedHeight += image.getHeight();
}
}
// Save resultant image
newImage.save(outputPath);
}
}
System.out.println("Done");
}
}

このサンプル コードは、Java* で *画像コンバイナーを開発するために使用できます。 JpegImage クラスのさまざまなメソッドを利用して、出力画像を作成し、画像のサイズを確認します。次に、四角形を作成し、Save メソッドを使用して出力イメージを保存します。ただし、JpegOptions クラスによって公開されるメソッドを使用して、スケーリング品質、RGB カラー プロファイル、サンプル丸めモードなどの出力イメージのさまざまな特性を設定できます。あなたの要件に従って。

この記事では、Java でのイメージのマージ の実行について学習しました。画像のトリミングについて学習することに興味がある場合は、Javaで画像をトリミングする方法 の記事をお読みください。

 日本語