{"id":73,"date":"2008-05-01T17:05:31","date_gmt":"2008-05-01T21:05:31","guid":{"rendered":"http:\/\/www.prglab.com\/blog\/p\/73"},"modified":"2009-04-20T15:50:49","modified_gmt":"2009-04-20T19:50:49","slug":"%e4%bd%bf%e7%94%a8java%e5%b0%86%e5%bd%a9%e8%89%b2%e5%9b%be%e7%89%87%e5%8f%98%e4%b8%ba1bit%e9%bb%91%e7%99%bd%e5%9b%be%e7%89%87","status":"publish","type":"post","link":"https:\/\/www.prglab.com\/blog\/p\/73","title":{"rendered":"\u4f7f\u7528JAVA\u5c06\u5f69\u8272\u56fe\u7247\u53d8\u4e3a1bit\u9ed1\u767d\u56fe\u7247"},"content":{"rendered":"<p>\u6700\u8fd1\u505a\u7684project\u9700\u8981\u6d89\u53ca\u5230\u5bf9\u626b\u63cf\u56fe\u7247\u7684\u6587\u5b57\u8bc6\u522b\uff0c\u9700\u8981\u5c06\u626b\u63cf\u5f97\u5230\u7684\u5f69\u8272\u56fe\u7247\u53d8\u4e3a1bit\u7684\u9ed1\u767d\u56fe\u7247\u3002<\/p>\n<p>\u641c\u7d22\u5230\u5f88\u591a\u89e3\u51b3\u65b9\u6848\uff0c\u57fa\u672c\u7684\u5f52\u7eb3\u4e3a\u4e24\u4e2a\u6b65\u9aa4\uff0c\u9996\u5148\u8981\u5c06\u5f69\u8272\u56fe\u7247\u53d8\u4e3a8bit\u7070\u5ea6\u56fe\u7247(grayscale image)\uff0c\u7136\u540e\u518d\u8fdb\u4e00\u6b65\u53d8\u4e3a1bit\u7684\u5355\u8272\u56fe\u7247(1bit monochrome)\u3002<\/p>\n<h3>\u65b9\u6848\u4e00\uff1a\u4f7f\u7528ColorConvert\u548cDithering\u64cd\u4f5c<\/h3>\n<h4>1\u3001\u4ee3\u7801<\/h4>\n<p>\u76ee\u524dJAVA\u6700\u65b0\u7684\u56fe\u7247\u5904\u7406\u5305\u5e94\u8be5\u662fJAI(Java Advanced Imaging)\uff0c\u5728<a href=\"http:\/\/java.sun.com\/products\/java-media\/jai\/forDevelopers\/jaifaq.html#1bit\" target=\"_blank\">JAI API\u7684FAQ\u9875\u9762<\/a>\u4e0a\uff0c\u627e\u5230\u4e86\u4e0b\u9762\u7684\u65b9\u6cd5\u6765\u5b8c\u6210\u8fd9\u4e24\u6b65\u5de5\u4f5c\uff1a<\/p>\n<table style=\"border-color: #aaaaaa; border-width: 1px; height: 237px;\" border=\"0\" width=\"566\">\n<tbody>\n<tr>\n<td style=\"border: 1px solid #cccccc; background-color: #eeeeee\">\u5c06\u5f69\u8272\u56fe\u7247\u8f6c\u6362\u4e3a8bit\u7070\u5ea6\u56fe\u7247(\u4f7f\u7528ColorConvert\u989c\u8272\u8f6c\u6362\u64cd\u4f5c)<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #cccccc\">[java]<br \/>\npublic RenderedImage convertTo8BitGray(RenderedImage colorImage){<br \/>\nParameterBlock pb = new ParameterBlock();<br \/>\npb.addSource(colorImage);<br \/>\nColorModel cm = new ComponentColorModel(<br \/>\nColorSpace.getInstance(ColorSpace.CS_GRAY),<br \/>\nnew int[]{8},<br \/>\nfalse,<br \/>\nfalse,<br \/>\nTransparency.OPAQUE,<br \/>\nDataBuffer.TYPE_BYTE);<br \/>\npb.add(cm);<br \/>\nRenderedImage grayImage = JAI.create(&#8220;ColorConvert&#8221;, pb);<br \/>\nreturn grayImage;<br \/>\n}[\/java]<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<table style=\"border-color: #aaaaaa; border-width: 1px; height: 40px;\" border=\"0\" width=\"566\">\n<tbody>\n<tr>\n<td style=\"border: 1px solid #cccccc; background-color: #eeeeee\">\u5c068bit\u7070\u5ea6\u56fe\u7247\u8f6c\u6362\u4e3a1bit\u9ed1\u767d\u56fe\u7247 (\u4f7f\u7528errordiffusion\u6216ordereddither\u64cd\u4f5c)<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #cccccc\">[java]<br \/>\npublic  RenderedImage applyDithering(RenderedImage grayImage, boolean isErrorDiffusion){<br \/>\n\/\/ Load the ParameterBlock for the dithering operation<br \/>\n\/\/ and set the operation name.<br \/>\nParameterBlock pb = new ParameterBlock();<br \/>\npb.addSource(grayImage);<br \/>\nString opName = null;<br \/>\nif(isErrorDiffusion) {<br \/>\nopName = &#8220;errordiffusion&#8221;;<br \/>\nLookupTableJAI lut = new LookupTableJAI(new byte[] {(byte)0x00, (byte)0xff});<br \/>\npb.add(lut);<br \/>\npb.add(KernelJAI.ERROR_FILTER_FLOYD_STEINBERG);<br \/>\n} else {<br \/>\nopName = &#8220;ordereddither&#8221;;<br \/>\nColorCube cube = ColorCube.createColorCube(DataBuffer.TYPE_BYTE,<br \/>\n0, new int[] {2}); \/\/\u5c1d\u8bd5\u6539\u53d82\u4e3a\u5176\u5b83\u503c\uff0c\u53ef\u4ee5\u5f97\u5230\u4e0d\u540c\u6548\u679c<br \/>\npb.add(cube);<br \/>\npb.add(KernelJAI.DITHER_MASK_441);<br \/>\n}<br \/>\n\/\/ Create a layout containing an IndexColorModel which maps<br \/>\n\/\/ zero to zero and unity to 255.<br \/>\nImageLayout layout = new ImageLayout();<br \/>\nbyte[] map = new byte[] {(byte)0x00, (byte)0xff};<br \/>\nColorModel cm = new IndexColorModel(1, 2, map, map, map);<br \/>\nlayout.setColorModel(cm);<br \/>\n\/\/ Create a hint containing the layout.<br \/>\nRenderingHints hints = new RenderingHints(JAI.KEY_IMAGE_LAYOUT, layout);<br \/>\n\/\/ Dither the image.<br \/>\nbwImage = JAI.create(opName, pb, hints);<br \/>\nreturn bwImage;<br \/>\n}<br \/>\n[\/java]<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>2\u3001\u95ee\u9898<\/h4>\n<p>\u8fd9\u6bb5sample code\u5728\u5f88\u591a\u5730\u65b9\u90fd\u88ab\u63d0\u5230\u8fc7\uff0c\u6211\u4e0d\u77e5\u9053\u522b\u4eba\u662f\u5426\u80fd\u591f\u8ba9\u5b83\u987a\u5229\u6267\u884c\uff0c\u4f46\u662f\u5728\u6211\u7684\u673a\u5668\u4e0a\u662f\u4e0d\u884c\u7684\u3002<\/p>\n<p>\u95ee\u9898\u4e3b\u8981\u51fa\u5728\u7b2c\u4e00\u4e2aconvertTo8BitGray()\u51fd\u6570\u91cc\u7684ColorConvert\u64cd\u4f5c\u4e0a\u3002\u5728\u6267\u884c\u5b8cColorConvert\u4e4b\u540e\uff0c\u56fe\u7247\u7684ColorModel\u7684numberOfComponents\u53d8\u4e3a1\u3002\u6211\u7684\u7406\u89e3\u662f\u56e0\u4e3a\u53d8\u4e3a\u7070\u5ea6\u56fe\u7247\uff0c\u53ea\u6709\u4e00\u4e2a\u8272\u4e86\u3002<\/p>\n<p>\u7136\u800c\u4e0d\u77e5\u4e3a\u4ec0\u4e48\uff0c\u56fe\u7247\u7684SampleModel\u7684numberOfBands\u5374\u4ecd\u7136\u4fdd\u6301\u4e863\uff0c\u8fd9\u5c31\u9020\u6210\u4e86SampleModel\u548cColorModel \u4e0d\u517c\u5bb9\uff0c\u6240\u4ee5\u7a0b\u5e8f\u603b\u662f\u629b\u51fa<span style=\"color: #ff0000;\">java.lang.IllegalArgumentException: The specified ColorModel is incompatible with the image SampleModel&#8230;<\/span>\u7684\u9519\u8bef\u3002<\/p>\n<p><!--more--><\/p>\n<h3>\u65b9\u6848\u4e8c\uff1a\u4f7f\u7528\u91cd\u753b\u5230\u7070\u5ea6\u56fe\u7247\u4e0a\u7684\u65b9\u6cd5<\/h3>\n<p>\u540e\u6765\u6211\u5728Code Beach\u770b\u5230\u4e86<a href=\"http:\/\/blog.codebeach.com\/2008\/03\/convert-color-image-to-gray-scale-image.html\" target=\"_blank\">\u8fd9\u7bc7\u6587\u7ae0<\/a>\uff0c\u8bd5\u7528\u4e86\u91cc\u9762\u7684\u7b2c\u4e8c\u4e2a\u65b9\u6cd5 &#8212; \u5c06\u5f69\u8272\u56fe\u7247\u753b\u5230\u4e00\u4e2a\u7070\u5ea6\u56fe\u7247\u4e0a\u7684\u65b9\u6cd5\uff0c\u7ec8\u4e8e\u53ef\u4ee5\u6210\u529f\u5c06\u5f69\u8272\u56fe\u7247\u8f6c\u5316\u4e3a8bit\u7684\u7070\u5ea6\u56fe\u7247\u3002\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<p>[java]<br \/>\nBufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);<br \/>\nGraphics g = image.getGraphics();<br \/>\ng.drawImage(colorImage, 0, 0, null);<br \/>\ng.dispose();<br \/>\n[\/java]<\/p>\n<p>\u8f6c\u6362\u7684\u6548\u679c\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/p>\n<table style=\"height: 22px;\" border=\"0\" width=\"24\">\n<tbody>\n<tr>\n<td>\u8f6c\u6362\u524d\uff1a\u5f69\u8272\u56fe\u7247<br \/>\n<a title=\"rose.jpg\" rel=\"attachment wp-att-74\" href=\"http:\/\/www.prglab.com\/blog\/p\/73\/rosejpg\"><br \/>\n<img src=\"http:\/\/www.prglab.com\/blog\/wp-content\/uploads\/rose.jpg\" alt=\"rose.jpg\" width=\"250\" \/><\/a><\/td>\n<td>\u8f6c\u6362\u540e\uff1a\u7070\u5ea6\u56fe\u7247<br \/>\n<a title=\"rose_gray.jpg\" rel=\"attachment wp-att-77\" href=\"http:\/\/www.prglab.com\/blog\/p\/73\/rose_grayjpg\"><br \/>\n<img src=\"http:\/\/www.prglab.com\/blog\/wp-content\/uploads\/rose_gray.jpg\" alt=\"rose_gray.jpg\" width=\"250\" \/><\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><!--more--><\/p>\n<h3>\u4f7f\u7528Binarize\u5c068bit\u7070\u5ea6\u56fe\u7247\u8f6c\u6362\u4e3a1bit\u9ed1\u767d\u56fe\u7247<\/h3>\n<p>\u5c06\u5f69\u8272\u56fe\u7247\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u7247\u540e\uff0c\u4ecd\u7136\u53ef\u4ee5\u7528\u524d\u9762\u7a0b\u5e8f\u4f8b\u5b50\u4e2d\u7684applyDithering()\u65b9\u6cd5\u6765\u5c068bit\u7070\u5ea6\u56fe\u7247\u8fdb\u4e00\u6b65\u8f6c\u6362\u4e3a1bit\u9ed1\u767d\u56fe\u7247\uff0c\u5f97\u5230\u7684\u6548\u679c\u6bd4\u8f83\u8c61\u62a5\u7eb8\u4e0a\u7684\u9ed1\u767d\u56fe\u7247\uff0c\u4fdd\u7559\u4e86\u5f88\u591a\u7070\u5ea6\u7684\u5c42\u6b21\u3002<br \/>\n\u4e3a\u4e86\u80fd\u591f\u66f4\u8fdb\u4e00\u6b65\u5f97\u5230\u7248\u753b\u6548\u679c\u7684\u56fe\u7247\uff0c\u53ef\u4ee5\u4f7f\u7528Binarize\u64cd\u4f5c\u3002\u6539\u53d8Binarize\u64cd\u4f5c\u7684\u57df\u503c(threshold)\u4e5f\u53ef\u4ee5\u5f97\u5230\u4e0d\u540c\u6548\u679c\u3002<br \/>\n<strong>\u4ee3\u7801\uff1a<\/strong><br \/>\n[java]<br \/>\n\/***<br \/>\n* Binarize image (convert image to 1 bit black and white)<br \/>\n* \u8f93\u5165\u56fe\u7247\u5fc5\u987b\u4e3a\u7070\u5ea6\u56fe\u7247\uff0c\u5426\u5219\u4f1a\u51fa\u9519\u3002<br \/>\n*\/<br \/>\npublic RenderedImage applyBinarize(RenderedImage grayImage) {<br \/>\n\/\/ Generate a histogram.<br \/>\nHistogram histogram =<br \/>\n(Histogram)JAI.create(&#8220;histogram&#8221;, grayImage).getProperty(&#8220;histogram&#8221;);<br \/>\n\/\/ Get a threshold equal to the median.<br \/>\ndouble[] threshold = histogram.getPTileThreshold(0.4); \/\/\u6539\u53d8\u57df\u503c\u53ef\u4ee5\u5f97\u5230\u4e0d\u540c\u6548\u679c<br \/>\n\/\/ Binarize the image.<br \/>\nRenderedImage bwImage =<br \/>\nJAI.create(&#8220;binarize&#8221;, grayImage, new Double(threshold[0]));<br \/>\nreturn bwImage;<br \/>\n}\/\/function applyBinarize<br \/>\n[\/java]<\/p>\n<p><strong>\u6548\u679c\uff1a<\/strong><\/p>\n<table border=\"0\">\n<tbody>\n<tr>\n<td>\u4f7f\u7528Dithering\u7684\u6548\u679c<\/td>\n<td>\u4f7f\u7528Binarizing\u7684\u6548\u679c<\/td>\n<\/tr>\n<tr>\n<td><a title=\"rose_dithering.jpg\" rel=\"attachment wp-att-76\" href=\"http:\/\/www.prglab.com\/blog\/p\/73\/rose_ditheringjpg\"><img src=\"http:\/\/www.prglab.com\/blog\/wp-content\/uploads\/rose_dithering.jpg\" alt=\"rose_dithering.jpg\" width=\"250\" \/><\/a><\/td>\n<td><a title=\"rose_bw.jpg\" rel=\"attachment wp-att-75\" href=\"http:\/\/www.prglab.com\/blog\/p\/73\/rose_bwjpg\"><img src=\"http:\/\/www.prglab.com\/blog\/wp-content\/uploads\/rose_bw.jpg\" alt=\"rose_bw.jpg\" width=\"250\" \/><\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u505a\u7684project\u9700\u8981\u6d89\u53ca\u5230\u5bf9\u626b\u63cf\u56fe\u7247\u7684\u6587\u5b57\u8bc6\u522b\uff0c\u9700\u8981\u5c06\u626b\u63cf\u5f97\u5230\u7684\u5f69\u8272\u56fe\u7247\u53d8\u4e3a1bit\u7684\u9ed1\u767d\u56fe\u7247\u3002 \u641c\u7d22\u5230\u5f88\u591a\u89e3\u51b3\u65b9\u6848\uff0c\u57fa\u672c\u7684\u5f52\u7eb3\u4e3a\u4e24\u4e2a\u6b65\u9aa4\uff0c\u9996\u5148\u8981\u5c06\u5f69\u8272\u56fe\u7247\u53d8\u4e3a8bit\u7070\u5ea6\u56fe\u7247(grayscale image)\uff0c\u7136\u540e\u518d\u8fdb\u4e00\u6b65\u53d8\u4e3a1bit\u7684\u5355\u8272\u56fe\u7247(1bit monochrome)\u3002 \u65b9\u6848\u4e00\uff1a\u4f7f\u7528ColorConvert\u548cDithering\u64cd\u4f5c 1\u3001\u4ee3\u7801 \u76ee\u524dJAVA\u6700\u65b0\u7684\u56fe\u7247\u5904\u7406\u5305\u5e94\u8be5\u662fJAI(Java Advanced Imaging)\uff0c\u5728JAI API\u7684FAQ\u9875\u9762\u4e0a\uff0c\u627e\u5230\u4e86\u4e0b\u9762\u7684\u65b9\u6cd5\u6765\u5b8c\u6210\u8fd9\u4e24\u6b65\u5de5\u4f5c\uff1a \u5c06\u5f69\u8272\u56fe\u7247\u8f6c\u6362\u4e3a8bit\u7070\u5ea6\u56fe\u7247(\u4f7f\u7528ColorConvert\u989c\u8272\u8f6c\u6362\u64cd\u4f5c) [java] public RenderedImage convertTo8BitGray(RenderedImage colorImage){ ParameterBlock pb = new ParameterBlock(); pb.addSource(colorImage); ColorModel cm = new ComponentColorModel( ColorSpace.getInstance(ColorSpace.CS_GRAY), new int[]{8}, false, false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE); pb.add(cm); RenderedImage grayImage = JAI.create(&#8220;ColorConvert&#8221;, pb); return grayImage; }[\/java] \u5c068bit\u7070\u5ea6\u56fe\u7247\u8f6c\u6362\u4e3a1bit\u9ed1\u767d\u56fe\u7247 (\u4f7f\u7528errordiffusion\u6216ordereddither\u64cd\u4f5c) [java] public RenderedImage applyDithering(RenderedImage grayImage, boolean isErrorDiffusion){ \/\/ Load the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[9],"tags":[],"_links":{"self":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/posts\/73"}],"collection":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/comments?post=73"}],"version-history":[{"count":3,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/posts\/73\/revisions"}],"predecessor-version":[{"id":91,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/posts\/73\/revisions\/91"}],"wp:attachment":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/media?parent=73"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/categories?post=73"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/tags?post=73"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}