Java ImageIO.write() Runtime Exception -
i trying follow example here: http://docs.oracle.com/javase/tutorial/displaycode.html?code=http://docs.oracle.com/javase/tutorial/essential/concurrency/examples/forkblur.java
/* * copyright (c) 2010, 2013, oracle and/or affiliates. rights reserved. * * redistribution , use in source , binary forms, or without * modification, permitted provided following conditions * met: * * - redistributions of source code must retain above copyright * notice, list of conditions , following disclaimer. * * - redistributions in binary form must reproduce above copyright * notice, list of conditions , following disclaimer in * documentation and/or other materials provided distribution. * * - neither name of oracle or names of * contributors may used endorse or promote products derived * software without specific prior written permission. * * software provided copyright holders , contributors "as * is" , express or implied warranties, including, not limited to, * implied warranties of merchantability , fitness particular * purpose disclaimed. in no event shall copyright owner or * contributors liable direct, indirect, incidental, special, * exemplary, or consequential damages (including, not limited to, * procurement of substitute goods or services; loss of use, data, or * profits; or business interruption) caused , on theory of * liability, whether in contract, strict liability, or tort (including * negligence or otherwise) arising in way out of use of * software, if advised of possibility of such damage. */ import java.awt.image.bufferedimage; import java.io.file; import java.util.concurrent.forkjoinpool; import java.util.concurrent.recursiveaction; import javax.imageio.imageio; /** * forkblur implements simple horizontal image blur. averages pixels in * source array , writes them destination array. sthreshold value * determines whether blurring performed directly or split 2 * tasks. * * not recommended way blur images; intended * illustrate use of fork/join framework. */ public class forkblur extends recursiveaction { private int[] msource; private int mstart; private int mlength; private int[] mdestination; private int mblurwidth = 15; // processing window size, should odd. public forkblur(int[] src, int start, int length, int[] dst) { msource = src; mstart = start; mlength = length; mdestination = dst; } // average pixels source, write results destination. protected void computedirectly() { int sidepixels = (mblurwidth - 1) / 2; (int index = mstart; index < mstart + mlength; index++) { // calculate average. float rt = 0, gt = 0, bt = 0; (int mi = -sidepixels; mi <= sidepixels; mi++) { int mindex = math.min(math.max(mi + index, 0), msource.length - 1); int pixel = msource[mindex]; rt += (float) ((pixel & 0x00ff0000) >> 16) / mblurwidth; gt += (float) ((pixel & 0x0000ff00) >> 8) / mblurwidth; bt += (float) ((pixel & 0x000000ff) >> 0) / mblurwidth; } // re-assemble destination pixel. int dpixel = (0xff000000) | (((int) rt) << 16) | (((int) gt) << 8) | (((int) bt) << 0); mdestination[index] = dpixel; } } protected static int sthreshold = 10000; @override protected void compute() { if (mlength < sthreshold) { computedirectly(); return; } int split = mlength / 2; invokeall(new forkblur(msource, mstart, split, mdestination), new forkblur(msource, mstart + split, mlength - split, mdestination)); } // plumbing follows. public static void main(string[] args) throws exception { string srcname = "awesome_face.jpg"; file srcfile = new file(srcname); bufferedimage image = imageio.read(srcfile); system.out.println("source image: " + srcname); bufferedimage blurredimage = blur(image); string dstname = "blurred-awesome-face.jpg"; file dstfile = new file(dstname); imageio.write(blurredimage, "jpg", dstfile); system.out.println("output image: " + dstname); } public static bufferedimage blur(bufferedimage srcimage) { int w = srcimage.getwidth(); int h = srcimage.getheight(); int[] src = srcimage.getrgb(0, 0, w, h, null, 0, w); int[] dst = new int[src.length]; system.out.println("array size " + src.length); system.out.println("threshold " + sthreshold); int processors = runtime.getruntime().availableprocessors(); system.out.println(integer.tostring(processors) + " processor" + (processors != 1 ? "s " : " ") + "available"); forkblur fb = new forkblur(src, 0, src.length, dst); forkjoinpool pool = new forkjoinpool(); long starttime = system.currenttimemillis(); pool.invoke(fb); long endtime = system.currenttimemillis(); system.out.println("image blur took " + (endtime - starttime) + " milliseconds."); bufferedimage dstimage = new bufferedimage(w, h, bufferedimage.type_int_argb); dstimage.setrgb(0, 0, w, h, dst, 0, w); return dstimage; } } i copied code computer , compiled , ran , received following error:
source image: awesome_face.jpg array size 100000 threshold 10000 4 processors available image blur took 19 milliseconds. exception in thread "main" javax.imageio.iioexception: invalid argument native writeimage @ com.sun.imageio.plugins.jpeg.jpegimagewriter.writeimage(native method) @ com.sun.imageio.plugins.jpeg.jpegimagewriter.writeonthread(jpegimagewriter.java:1058) @ com.sun.imageio.plugins.jpeg.jpegimagewriter.write(jpegimagewriter.java:360) @ javax.imageio.imagewriter.write(imagewriter.java:615) @ javax.imageio.imageio.dowrite(imageio.java:1612) @ javax.imageio.imageio.write(imageio.java:1536) @ forkblur.main(forkblur.java:112) it looks problem @ line 112 in imageio.write() method. looked @ api method here: http://docs.oracle.com/javase/7/docs/api/javax/imageio/imageio.html understanding arguments write() okay messed @ runtime.
does know wrong program?
regards,
...
Comments
Post a Comment