canvas - How to crop and resize JavaFX Image? -
canvas - How to crop and resize JavaFX Image? -
i'm trying display big images on javafx canvas. resolution of single image 11980x8365. each image has corresponding world file , can utilize position images correctly. canvas size 800x600. need write whole image on canvas, , part of it.
here i've done far:
load total sized image file image object. calculate part of image show , calculate scale parameters correctly fit 800x600 canvas.so wanted utilize graphicscontext.drawimage(...) - draws current source rectangle of given image given destination rectangle of canvas.
for method calculated parameters correctly. problem image larger 2048x2048, , reason javafx tries draw image straight canvas using gpu (if understood correctly). that's when exception:
java.lang.nullpointerexception @ com.sun.prism.sw.swgraphics.drawtexture(swgraphics.java:686) @ com.sun.prism.sw.swgraphics.drawtexture(swgraphics.java:686) @ com.sun.prism.sw.swgraphics.drawtexture(swgraphics.java:665) @ com.sun.prism.sw.swgraphics.drawtexture(swgraphics.java:648) @ com.sun.javafx.sg.prism.ngcanvas.handlerenderop(ngcanvas.java:1228) @ com.sun.javafx.sg.prism.ngcanvas.renderstream(ngcanvas.java:997) @ com.sun.javafx.sg.prism.ngcanvas.rendercontent(ngcanvas.java:578) @ com.sun.javafx.sg.prism.ngnode.dorender(ngnode.java:2043) @ com.sun.javafx.sg.prism.ngnode.render(ngnode.java:1951) @ com.sun.javafx.sg.prism.nggroup.rendercontent(nggroup.java:225) @ com.sun.javafx.sg.prism.ngregion.rendercontent(ngregion.java:575) @ com.sun.javafx.sg.prism.ngnode.dorender(ngnode.java:2043) @ com.sun.javafx.sg.prism.ngnode.render(ngnode.java:1951) @ com.sun.javafx.sg.prism.nggroup.rendercontent(nggroup.java:225) @ com.sun.javafx.sg.prism.ngregion.rendercontent(ngregion.java:575) @ com.sun.javafx.sg.prism.ngnode.renderforclip(ngnode.java:2282) @ com.sun.javafx.sg.prism.ngnode.renderrectclip(ngnode.java:2176) @ com.sun.javafx.sg.prism.ngnode.renderclip(ngnode.java:2202) @ com.sun.javafx.sg.prism.cachefilter.impl_rendernodetocache(cachefilter.java:655) @ com.sun.javafx.sg.prism.cachefilter.render(cachefilter.java:561) @ com.sun.javafx.sg.prism.ngnode.rendercached(ngnode.java:2346) @ com.sun.javafx.sg.prism.ngnode.dorender(ngnode.java:2034) @ com.sun.javafx.sg.prism.ngnode.render(ngnode.java:1951) @ com.sun.javafx.sg.prism.nggroup.rendercontent(nggroup.java:225) @ com.sun.javafx.sg.prism.ngregion.rendercontent(ngregion.java:575) @ com.sun.javafx.sg.prism.ngnode.dorender(ngnode.java:2043) @ com.sun.javafx.sg.prism.ngnode.render(ngnode.java:1951) @ com.sun.javafx.sg.prism.nggroup.rendercontent(nggroup.java:225) @ com.sun.javafx.sg.prism.ngregion.rendercontent(ngregion.java:575) @ com.sun.javafx.sg.prism.ngnode.dorender(ngnode.java:2043) @ com.sun.javafx.sg.prism.ngnode.render(ngnode.java:1951) @ com.sun.javafx.sg.prism.nggroup.rendercontent(nggroup.java:225) @ com.sun.javafx.sg.prism.ngregion.rendercontent(ngregion.java:575) @ com.sun.javafx.sg.prism.ngnode.renderforclip(ngnode.java:2282) @ com.sun.javafx.sg.prism.ngnode.renderrectclip(ngnode.java:2176) @ com.sun.javafx.sg.prism.ngnode.renderclip(ngnode.java:2202) @ com.sun.javafx.sg.prism.ngnode.dorender(ngnode.java:2037) @ com.sun.javafx.sg.prism.ngnode.render(ngnode.java:1951) @ com.sun.javafx.sg.prism.nggroup.rendercontent(nggroup.java:225) @ com.sun.javafx.sg.prism.ngregion.rendercontent(ngregion.java:575) @ com.sun.javafx.sg.prism.ngnode.dorender(ngnode.java:2043) @ com.sun.javafx.sg.prism.ngnode.render(ngnode.java:1951) @ com.sun.javafx.sg.prism.nggroup.rendercontent(nggroup.java:225) @ com.sun.javafx.sg.prism.ngregion.rendercontent(ngregion.java:575) @ com.sun.javafx.sg.prism.ngnode.dorender(ngnode.java:2043) @ com.sun.javafx.sg.prism.ngnode.render(ngnode.java:1951) @ com.sun.javafx.sg.prism.nggroup.rendercontent(nggroup.java:225) @ com.sun.javafx.sg.prism.ngregion.rendercontent(ngregion.java:575) @ com.sun.javafx.sg.prism.ngnode.renderforclip(ngnode.java:2282) @ com.sun.javafx.sg.prism.ngnode.renderrectclip(ngnode.java:2176) @ com.sun.javafx.sg.prism.ngnode.renderclip(ngnode.java:2202) @ com.sun.javafx.sg.prism.ngnode.dorender(ngnode.java:2037) @ com.sun.javafx.sg.prism.ngnode.render(ngnode.java:1951) @ com.sun.javafx.sg.prism.nggroup.rendercontent(nggroup.java:225) @ com.sun.javafx.sg.prism.ngregion.rendercontent(ngregion.java:575) @ com.sun.javafx.sg.prism.ngnode.dorender(ngnode.java:2043) @ com.sun.javafx.sg.prism.ngnode.render(ngnode.java:1951) @ com.sun.javafx.sg.prism.nggroup.rendercontent(nggroup.java:225) @ com.sun.javafx.sg.prism.ngregion.rendercontent(ngregion.java:575) @ com.sun.javafx.sg.prism.ngnode.dorender(ngnode.java:2043) @ com.sun.javafx.sg.prism.ngnode.render(ngnode.java:1951) @ com.sun.javafx.tk.quantum.viewpainter.dopaint(viewpainter.java:469) @ com.sun.javafx.tk.quantum.viewpainter.paintimpl(viewpainter.java:317) @ com.sun.javafx.tk.quantum.uploadingpainter.run(uploadingpainter.java:132) @ java.util.concurrent.executors$runnableadapter.call(executors.java:511) @ java.util.concurrent.futuretask.runandreset(futuretask.java:308) @ com.sun.javafx.tk.renderjob.run(renderjob.java:58) @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1142) @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:617) @ com.sun.javafx.tk.quantum.quantumrenderer$pipelinerunnable.run(quantumrenderer.java:129) @ java.lang.thread.run(thread.java:744)
so want seek next crop , scale image in temporary object before sending canvas. can't find anywhere illustration how that. illustration found how crop image using writableimage, don't know how scale after cropping , convert image.
there number of ways can this, depending on info have available @ various points in process.
if know size of image on file before loading, , can compute scale factor, can scale load it:
double requiredwidth = ... ; double requiredheight = ... ; string imageurl = ... ; image image = new image(imageurl, requiredwidth, requiredheight, false, true);
the lastly 2 parameters preserveratio
, smooth
. latter forcefulness slower improve quality rescaling algorithm.
now can crop new writableimage
in post linked:
double x = ... ; double y = ... ; double width = ...; double height = ... ; writableimage croppedimage = new writableimage(image.getpixelreader(), x, y, width, height);
where x
, y
, width
, , height
defined cropped part (in scaled coordinates).
and can draw cropped image canvas:
graphicscontent.drawimage(croppedimage, canvasx, canvasy);
anther approach load whole image, , utilize imageview
create cropped, scaled view of it:
image fullimage = new image(imageurl); // define crop in image coordinates: rectangle2d croppedportion = new rectangle2d(x, y, width, height); // target width , height: double scaledwidth = ... ; double scaledheight = ... ; imageview imageview = new imageview(fullimage); imageview.setviewport(croppedportion); imageview.setfitwidth(scaledwidth); imageview.setfitheight(scaledheight); imageview.setsmooth(true);
now can create new image cropped version of original image taking snapshot of imageview
. this, need place imageview
off-screen scene:
pane pane = new pane(imageview); scene offscreenscene = new scene(pane); writableimage croppedimage = imageview.snapshot(null, null);
and can draw cropped image canvas before.
image canvas javafx-2 javafx-8
Comments
Post a Comment