ウェブサイトの完璧な画像スクリーンショットを作成するか、次の機能を使用してHTMLを直接画像に変換します GrabzItのJava API。 ただし、開始する前に、 URLToImage, HTMLToImage or FileToImage メソッド Save or SaveTo スクリーンショットを撮るには、メソッドを呼び出す必要があります。
Webページのスクリーンショットを撮るには1つのパラメーターが必要です HTMLを変換 into画像 ファイル。 次の例に示すように。
grabzIt.URLToImage("https://www.tesla.com"); //Then call the Save or SaveTo method
grabzIt.HTMLToImage("<html><body><h1>Hello World!</h1></body></html>"); //Then call the Save or SaveTo method
grabzIt.FileToImage("example.html"); //Then call the Save or SaveTo method
GrabzItのJava APIは、JPG、PNG、WEBP、BMP(8ビット、16ビット、24ビットまたは32ビット)およびTIFFを含むいくつかの形式で画像のスクリーンショットを取得できます。 画像のスクリーンショットのデフォルト形式はJPGです。 ただし、このような状況では一部のアプリではJPEG画像の品質が十分ではない場合があります。画像のスクリーンショットには品質とファイルサイズのバランスがとれるため、PNG形式が推奨されます。 以下の例は、PNG形式を使用して撮影された画像のスクリーンショットを示しています。
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret"); ImageOptions options = new ImageOptions(); options.setFormat(ImageFormat.PNG); grabzIt.URLToImage("https://www.tesla.com", options); //Then call the Save or SaveTo method grabzIt.SaveTo("result.png");
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret"); ImageOptions options = new ImageOptions(); options.setFormat(ImageFormat.PNG); grabzIt.HTMLToImage("<html><body><h1>Hello World!</h1></body></html>", options); //Then call the Save or SaveTo method grabzIt.SaveTo("result.png");
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret"); ImageOptions options = new ImageOptions(); options.setFormat(ImageFormat.PNG); grabzIt.FileToImage("example.html", options); //Then call the Save or SaveTo method grabzIt.SaveTo("result.png");
ブラウザーのサイズは、ほとんどすべてのタスクでデフォルトのブラウザーサイズで十分なので、ほとんどの場合、スクリーンショットをキャプチャするときに使用されるブラウザーウィンドウのサイズを指します。 ブラウザのサイズを設定するには、単に値を setBrowserWidth
および setBrowserHeight
の方法 ImageOptions とに提供されます。
画像のサイズを変更するのは簡単です。画像を歪めずに変更するのは少し難しいです。 プロセス全体を簡単にするために、これを使用することをお勧めします シンプルな画像寸法計算機.
画像の幅と高さをブラウザーの幅と高さ(デフォルトでは1366 x 728ピクセル)よりも大きくする場合、ブラウザーの幅と高さも一致するように増やす必要があります。
にカスタム識別子を渡すことができます 画像 次に示すメソッドを使用すると、この値はGrabzIt Javaハンドラーに返されます。 たとえば、このカスタム識別子はデータベース識別子であり、スクリーンショットを特定のデータベースレコードに関連付けることができます。
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret"); ImageOptions options = new ImageOptions(); options.setCustomId("123456"); grabzIt.URLToImage("https://www.tesla.com", options); //Then call the Save method grabzIt.Save("http://www.example.com/handler");
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret"); ImageOptions options = new ImageOptions(); options.setCustomId("123456"); grabzIt.HTMLToImage("<html><body><h1>Hello World!</h1></body></html>", options); //Then call the Save method grabzIt.Save("http://www.example.com/handler");
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret"); ImageOptions options = new ImageOptions(); options.setCustomId("123456"); grabzIt.FileToImage("example.html", options); //Then call the Save method grabzIt.Save("http://www.example.com/handler");
GrabzItを使用すると、Webページ全体のフルスクリーンのスクリーンショットを撮ることができます。これを行うには、-1を渡す必要があります setBrowserHeight
方法。 画像がブラウザのサイズと一致するようにするには、-1を setHeight
および setWidth
方法。
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret"); ImageOptions options = new ImageOptions(); options.setBrowserHeight(-1); options.setWidth(-1); options.setHeight(-1); grabzIt.URLToImage("https://www.tesla.com", options); //Then call the Save or SaveTo method grabzIt.SaveTo("result.jpg");
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret"); ImageOptions options = new ImageOptions(); options.setBrowserHeight(-1); options.setWidth(-1); options.setHeight(-1); grabzIt.HTMLToImage("<html><body><h1>Hello World!</h1></body></html>", options); //Then call the Save or SaveTo method grabzIt.SaveTo("result.jpg");
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret"); ImageOptions options = new ImageOptions(); options.setBrowserHeight(-1); options.setWidth(-1); options.setHeight(-1); grabzIt.FileToImage("example.html", options); //Then call the Save or SaveTo method grabzIt.SaveTo("result.jpg");
トリミングされていないサムネイルを返すこともできますが、大きな画像が作成される可能性があることに注意してください。 これを行うには、-1を渡します setHeight
および setWidth
メソッド。 -1が渡されたディメンションは切り取られません。
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret"); ImageOptions options = new ImageOptions(); options.setWidth(-1); options.setHeight(-1); grabzIt.URLToImage("https://www.tesla.com", options); //Then call the Save or SaveTo method grabzIt.SaveTo("result.jpg");
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret"); ImageOptions options = new ImageOptions(); options.setWidth(-1); options.setHeight(-1); grabzIt.HTMLToImage("<html><body><h1>Hello World!</h1></body></html>", options); //Then call the Save or SaveTo method grabzIt.SaveTo("result.jpg");
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret"); ImageOptions options = new ImageOptions(); options.setWidth(-1); options.setHeight(-1); grabzIt.FileToImage("example.html", options); //Then call the Save or SaveTo method grabzIt.SaveTo("result.jpg");
これらの特別な値を使用すると、必要に応じてWebページ全体のフルスケールバージョンのスクリーンショットを作成できます。
GrabzItでは、次のようなidまたはclass属性を持っている限り、HTML要素のスクリーンショットを撮ることができます。 div
or span
タグを付け、そのすべてのコンテンツをキャプチャします。 これを行うには、スクリーンショットを撮るHTML要素を CSSセレクター.
... <div id="features"> <img src="http://www.example.com/football.jpg"/><h3>Local Team Loses</h3> </div> ...
次の例では、ID「features」を持つdivがキャプチャされ、250 x 250px JPEGイメージとして出力されます。
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret"); // The 250 parameters indicates that image should be sized to 250 x 250 px ImageOptions options = new ImageOptions(); options.setWidth(250); options.setHeight(250); options.setFormat(ImageFormat.JPG); options.setTargetElement("#features"); grabzIt.URLToImage("http://www.bbc.co.uk/news", options); //Then call the Save or SaveTo method grabzIt.SaveTo("result.jpg");
次の例では、再び「機能」divをキャプチャしますが、今回はdivとまったく同じサイズのJPEGイメージを出力します。
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret"); // The -1 indicates that image should not be cropped ImageOptions options = new ImageOptions(); options.setWidth(-1); options.setHeight(-1); options.setBrowserHeight(-1); options.setFormat(ImageFormat.JPG); options.setTargetElement("#features"); grabzIt.URLToImage("http://www.bbc.co.uk/news", options); //Then call the Save or SaveTo method grabzIt.SaveTo("result.jpg");