Cree capturas de pantalla de imágenes perfectas de sitios web o convierta HTML directamente a imágenes utilizando las siguientes funciones de API Java de GrabzIt. Sin embargo, antes de comenzar, recuerde que después de llamar al URLToImage, HTMLToImage or FileToImage métodos de Save or SaveTo Se debe llamar al método para tomar la captura de pantalla.
Solo se requiere un parámetro para tomar una captura de pantalla de una página web o convertir HTML into una imagen archivo. Como se muestra en el siguiente ejemplo.
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
La API Java de GrabzIt puede tomar capturas de pantalla de imágenes en varios formatos, incluidos JPG, PNG, WEBP, BMP (bit 8, bit 16, bit 24 o bit 32) y TIFF. El formato predeterminado para las capturas de pantalla de imágenes es JPG. Sin embargo, la calidad de una imagen JPEG puede no ser lo suficientemente buena para algunas aplicaciones; en estas circunstancias, se recomienda el formato PNG para las capturas de pantalla de la imagen, ya que proporciona un buen equilibrio entre la calidad y el tamaño del archivo. El siguiente ejemplo muestra una captura de pantalla de imagen tomada con el formato 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");
El tamaño del navegador se refiere al tamaño de la ventana del navegador que se utilizará al capturar la captura de pantalla; en la mayoría de los casos, no es necesario configurarlo, ya que el tamaño predeterminado del navegador será suficiente para casi todas las tareas. Para establecer el tamaño del navegador, simplemente pase un valor a setBrowserWidth
y setBrowserHeight
métodos de la ImageOptions clase.
Cambiar el tamaño de una imagen es fácil, hacerlo sin distorsionar la imagen es un poco más difícil. Para simplificar todo el proceso, le recomendamos que utilice este calculadora de dimensión de imagen simple.
Si desea aumentar el ancho y alto de la imagen a un tamaño mayor que el ancho y alto del navegador, que por defecto es 1366 por 728 píxeles, el ancho y la altura del navegador también deben aumentarse para que coincidan.
Puede pasar un identificador personalizado a imagen métodos como se muestra a continuación, este valor se devuelve a su controlador GrabzIt Java. Por ejemplo, este identificador personalizado podría ser un identificador de base de datos, lo que permite asociar una captura de pantalla con un registro de base de datos particular.
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 le permite tomar una captura de pantalla completa de una página web completa para hacer esto, necesita pasar un -1 al setBrowserHeight
método. Para garantizar que la imagen coincida con el tamaño del navegador, pase un -1 al setHeight
y setWidth
métodos.
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");
También puede devolver miniaturas que no estén recortadas, pero tenga en cuenta que esto puede crear imágenes grandes. Para hacer esto, pase un -1 al setHeight
y/o setWidth
métodos No se recortará ninguna dimensión que pase un -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");
¡Usar estos valores especiales significa que puede crear una captura de pantalla que sea una versión a escala completa de toda la página web si lo desea!
GrabzIt le permite tomar una captura de pantalla de cualquier elemento HTML siempre que tenga un atributo de identificación o clase, como un div
or span
etiquetar y capturar todo su contenido. Para hacer esto, el elemento HTML que desea capturar debe especificarse como un Selector de CSS.
... <div id="features"> <img src="http://www.example.com/football.jpg"/><h3>Local Team Loses</h3> </div> ...
En el ejemplo a continuación, el div con la identificación "características" se captura y se muestra como una imagen 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");
El siguiente ejemplo vuelve a capturar el div "características" pero esta vez genera una imagen JPEG que tiene exactamente el mismo tamaño que el div.
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");