画像ファイルの作成

Aspose.PSD for Java を使用すると、開発者は独自の画像を作成できます。Image クラスが公開する static Create メソッドを使用して新しい画像を作成します。必要なのは、出力画像形式のための ImageOptions ネームスペースのクラスのオブジェクトを供給するだけです。画像ファイルを作成するには、まず ImageOptions ネームスペースのクラスのインスタンスを作成します。これらのクラスは出力画像形式を決定します。以下に、ImageOptions ネームスペースからのいくつかのクラスが示されています(現在、PSD ファイル形式ファミリのみが作成にサポートされています):

PsdOptions は PSD ファイルを作成するためのオプションを設定します。出力パスを設定するか、ストリームを設定することで画像ファイルを作成できます。

パスを設定して作成

ImageOptions ネームスペースから PsdOptions を作成し、さまざまなプロパティを設定します。設定する最も重要なプロパティは、Source プロパティです。このプロパティは、画像データがどこにあるかを指定します(ファイルまたはストリーム)。以下の例では、ソースはファイルです。プロパティを設定した後、オブジェクトと幅と高さパラメータを1つの static Create メソッドに渡します。幅と高さはピクセル単位で定義されます。

String dataDir = Utils.getDataDir(CreatingbySettingPath.class) + "DrawingAndFormattingImages/";
String desName = dataDir + "CreatingAnImageBySettingPath_out.psd";
// Creates an instance of PsdOptions and set its various properties
PsdOptions psdOptions = new PsdOptions();
psdOptions.setCompressionMethod(CompressionMethod.RLE);
// Define the source property for the instance of PsdOptions. Second boolean parameter determines if the file is temporal or not
psdOptions.setSource(new FileCreateSource(desName, false));
// Creates an instance of Image and call Create method by passing the PsdOptions object
try (Image image = Image.create(psdOptions, 500, 500)) {
image.save();
}

ストリームを使用して作成

ストリームを使用して画像を作成するプロセスは、パスを使用する場合と同じです。唯一の違いは、Stream オブジェクトをそのコンストラクタに渡して StreamSource のインスタンスを作成し、Source プロパティに割り当てる必要があることです。

String dataDir = Utils.getDataDir(CreatingUsingStream.class) + "DrawingAndFormattingImages/";
String desName = dataDir + "CreatingImageUsingStream_out.bmp";
// Creates an instance of BmpOptions and set its various properties
BmpOptions imageOptions = new BmpOptions();
imageOptions.setBitsPerPixel(24);
// Create an instance of System.IO.Stream
FileCreateSource stream = new FileCreateSource(dataDir + "sample_out.bmp");
// Define the source property for the instance of BmpOptions Second boolean parameter determines if the Stream is disposed once get out of scope
imageOptions.setSource(stream);
// Creates an instance of Image and call Create method by passing the BmpOptions object
try (Image image = Image.create(imageOptions, 500, 500)) {
// Do some image processing
image.save(desName);
}

画像ファイルのオープン

開発者は、異なる目的で既存の PSD 画像ファイルを開くために Aspose.PSD for Java API を使用できます。例えば、イメージに効果を追加したり、既存のファイルを別の形式に変換したりすることができます。目的に関係なく、Aspose.PSD は既存のファイルを開くための 2 つの標準的な方法を提供します: ファイルからまたはストリームから。

ディスクからオープン

Image クラスが公開する static Load メソッドに、パスとファイル名をパラメータとして渡すことで、画像ファイルを開きます。

String dataDir = Utils.getDataDir(SavingtoDisk.class) + "Conversion/";
String sourceFile = dataDir + "sample.psd";
String destName = dataDir + "result.png";
// load PSD image and replace the non found fonts.
try (Image image = Image.load(sourceFile); PsdImage psdImage = (PsdImage) image) {
psdImage.save(destName, new PngOptions());
}

ストリームを使用して開く

開く必要がある画像がストリームとして保存されている場合は、Load メソッドのオーバーロードバージョンを使用します。これにより、ストリームを引数として受け入れ、画像を開くことができます。

String dataDir = Utils.getDataDir(LoadingFromStream.class) + "Conversion/";
String sourceFile = dataDir + "sample.psd";
String destName = dataDir + "result.png";
FileInputStream inputStream = new FileInputStream(sourceFile);
try (Image image = Image.load(inputStream);
PsdImage psdImage = (PsdImage) image) {
MemoryStream stream = new MemoryStream();
FileOutputStream outputStream = new FileOutputStream(sourceFile);
psdImage.save(outputStream, new PngOptions());
}

レイヤーとして画像を読み込む

この記事では、Aspose.PSD を使用して画像をレイヤーとして読み込む方法を示しています。Aspose.PSD API は、この目標を達成するための効率的で使いやすいメソッドを公開しています。Aspose.PSD は、PsdImage クラスの AddLayer メソッドを公開しており、PSD ファイルに画像をレイヤーとして追加することができます。

PSD に画像をレイヤーとして読み込む手順は以下の通りです:

String dataDir = Utils.getDataDir(LoadImageToPSD.class) + "PSD/";
String filePath = dataDir + "PsdExample.psd";
String outputFilePath = dataDir + "PsdResult.psd";
try (PsdImage image = new PsdImage(200, 200);
Image im = Image.load(filePath)) {
Layer layer = null;
try {
layer = new Layer((RasterImage) im, false);
image.addLayer(layer);
} catch (Exception e) {
if (layer != null) {
layer.dispose();
}
System.out.println(e);
}
}

画像ファイルの保存

Aspose.PSD を使用すると、画像ファイルをゼロから作成できます。また、既存の画像ファイルを編集する手段も提供します。画像が作成または変更されたら、通常はファイルがディスクに保存されます。Aspose.PSD は、画像をディスクに保存するためのメソッドを、パスを指定するかストリーム オブジェクトを使用する方法で提供します。

ディスクへの保存

Image クラスは画像オブジェクトを表し、このクラスは画像を作成、ロード、保存するために必要なすべてのツールを提供します。Save メソッドを使用して画像を保存します。Save メソッドのオーバーロードバージョンの 1 つは、ファイルの場所を文字列として受け入れます。

String dataDir = Utils.getDataDir(SavingtoDisk.class) + "Conversion/";
String sourceFile = dataDir + "sample.psd";
String destName = dataDir + "result.png";
// load PSD image and replace the non found fonts.
try (Image image = Image.load(sourceFile); PsdImage psdImage = (PsdImage) image) {
psdImage.save(destName, new PngOptions());
}

ストリームへの保存

Save メソッドの別のオーバーロードバージョンは、Stream オブジェクトを引数として受け入れ、画像ファイルをストリームに保存します。

画像が Image クラスの初期化時に提供されたパスまたはストリームで自動的に保存される場合、画像が Image クラスの初期化中に指定された CreateOptions のいずれかを指定して作成された場合は、Save メソッドを呼び出すことでパスまたはストリームに自動的に保存されます。

String dataDir = Utils.getDataDir(SavingtoStream.class) + "Conversion/";
String sourceFile = dataDir + "sample.psd";
String destName = dataDir + "result.png";
// load PSD image and replace the non found fonts.
try (Image image = Image.load(sourceFile); PsdImage psdImage = (PsdImage) image) {
FileOutputStream outputStream = new FileOutputStream(sourceFile);
psdImage.save(outputStream, new PngOptions());
}

欠落フォントの置換設定

開発者は、Aspose.PSD for Java API を使用して既存の PSD 画像ファイルをロードし、PSD ドキュメントをラスター画像(PNG、JPG、BMP 形式のいずれかに)として保存する場合、PSD ドキュメントを保存するときに現在のオペレーティングシステム内で見つからない欠落しているフォントすべてにデフォルトフォント名を設定するなど、さまざまな目的で使用できます。画像が変更された後、ファイルはディスクに保存されます。

String dataDir = Utils.getDataDir(SettingforReplacingMissingFonts.class) + "Conversion/";
String sourceFile = dataDir + "sample.psd";
String destName = dataDir + "result.png";
PsdLoadOptions loadOptions = new PsdLoadOptions();
loadOptions.setDefaultReplacementFont("Arial");
// load PSD image and replace the non found fonts.
try (Image image = Image.load(sourceFile, loadOptions);
PsdImage psdImage = (PsdImage) image) {
PngOptions Options = new PngOptions();
Options.setColorType(PngColorType.TruecolorWithAlpha);
psdImage.save(destName, Options);
}