{"id":66553,"date":"2025-05-03T16:42:09","date_gmt":"2025-05-03T08:42:09","guid":{"rendered":"https:\/\/fwq.ai\/blog\/66553\/"},"modified":"2025-05-03T16:42:09","modified_gmt":"2025-05-03T08:42:09","slug":"java%e4%b8%ad%e6%80%8e%e4%b9%88%e8%ae%b2%e5%9b%be%e7%89%87%e5%81%9a%e6%88%90%e6%95%b0%e7%bb%84-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/66553\/","title":{"rendered":"java\u4e2d\u600e\u4e48\u8bb2\u56fe\u7247\u505a\u6210\u6570\u7ec4"},"content":{"rendered":"<blockquote><p>\n  \u5c06\u56fe\u7247\u8f6c\u6362\u4e3a java \u4e2d\u7684\u6570\u7ec4\u7684\u65b9\u6cd5\u6709\u4ee5\u4e0b\u6b65\u9aa4\uff1a1. \u8bfb\u53d6\u56fe\u7247\u4e3a bufferedimage \u5bf9\u8c61\uff1b2. \u904d\u5386\u50cf\u7d20\uff0c\u63d0\u53d6\u989c\u8272\u503c\u5e76\u5b58\u50a8\u4e8e\u6570\u7ec4\uff1b3. \u53ef\u9009\uff0c\u63d0\u53d6\u989c\u8272\u5206\u91cf\uff08rgb\uff09\u5e76\u5b58\u50a8\u4e8e\u5355\u72ec\u7684\u6570\u7ec4\u4e2d\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/16\/2024111606184334033.jpg\" class=\"aligncenter\" title=\"java\u4e2d\u600e\u4e48\u8bb2\u56fe\u7247\u505a\u6210\u6570\u7ec4\u63d2\u56fe\" alt=\"java\u4e2d\u600e\u4e48\u8bb2\u56fe\u7247\u505a\u6210\u6570\u7ec4\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5c06\u56fe\u7247\u8f6c\u6362\u4e3a Java \u4e2d\u7684\u6570\u7ec4<\/strong><\/p>\n<p>\u5c06\u56fe\u7247\u8f6c\u6362\u4e3a Java \u4e2d\u7684\u6570\u7ec4\u7684\u65b9\u6cd5\u6709\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<p><strong>1. \u8bfb\u53d6\u56fe\u7247<\/strong><\/p>\n<pre>import java.awt.image.BufferedImage;\nimport javax.imageio.ImageIO;\n\n\/\/ \u8bfb\u53d6\u56fe\u7247\u5230 BufferedImage \u5bf9\u8c61\nBufferedImage image = ImageIO.read(new File(\"image.png\"));<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u63d0\u53d6\u50cf\u7d20<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>\/\/ \u83b7\u53d6\u56fe\u7247\u5bbd\u5ea6\u548c\u9ad8\u5ea6\nint width = image.getWidth();\nint height = image.getHeight();\n\n\/\/ \u521b\u5efa\u6570\u7ec4\u5b58\u50a8\u50cf\u7d20\nint[] pixels = new int[width * height];\n\n\/\/ \u904d\u5386\u6bcf\u4e2a\u50cf\u7d20\uff0c\u63d0\u53d6\u989c\u8272\u503c\nfor (int y = 0; y &lt; height; y++) {\n    for (int x = 0; x &lt; width; x++) {\n        pixels[y * width + x] = image.getRGB(x, y);\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u63d0\u53d6\u989c\u8272\u5206\u91cf\uff08\u53ef\u9009\uff09<\/strong><\/p>\n<pre>\/\/ \u5982\u679c\u9700\u8981\u63d0\u53d6\u989c\u8272\u5206\u91cf\uff0c\u8bf7\u521b\u5efa\u5355\u72ec\u7684\u6570\u7ec4\nint[][] red = new int[width][height];\nint[][] green = new int[width][height];\nint[][] blue = new int[width][height];\n\n\/\/ \u904d\u5386\u50cf\u7d20\uff0c\u63d0\u53d6 RGB \u989c\u8272\u5206\u91cf\nfor (int y = 0; y &lt; height; y++) {\n    for (int x = 0; x &lt; width; x++) {\n        int color = pixels[y * width + x];\n        red[x][y] = (color &gt;&gt; 16) &amp; 0xFF;\n        green[x][y] = (color &gt;&gt; 8) &amp; 0xFF;\n        blue[x][y] = color &amp; 0xFF;\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<pre>import java.awt.image.BufferedImage;\nimport javax.imageio.ImageIO;\n\npublic class ImageToArray {\n\n    public static void main(String[] args) throws Exception {\n        BufferedImage image = ImageIO.read(new File(\"image.png\"));\n\n        int width = image.getWidth();\n        int height = image.getHeight();\n        int[] pixels = new int[width * height];\n\n        for (int y = 0; y &lt; height; y++) {\n            for (int x = 0; x &lt; width; x++) {\n                pixels[y * width + x] = image.getRGB(x, y);\n            }\n        }\n\n        \/\/ \u6b64\u65f6\uff0cpixels \u6570\u7ec4\u5305\u542b\u56fe\u50cf\u7684\u50cf\u7d20\u503c\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u4e2d\u600e\u4e48\u8bb2\u56fe\u7247\u505a\u6210\u6570\u7ec4\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8IDCBABY\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5c06\u56fe\u7247\u8f6c\u6362\u4e3a java \u4e2d\u7684\u6570\u7ec4\u7684\u65b9\u6cd5\u6709\u4ee5\u4e0b\u6b65\u9aa4\uff1a1. \u8bfb\u53d6\u56fe\u7247\u4e3a bufferedimage \u5bf9\u8c61\uff1b2. \u904d\u5386\u50cf\u7d20\uff0c\u63d0\u53d6\u989c\u8272\u503c\u5e76\u5b58\u50a8\u4e8e\u6570\u7ec4\uff1b3. \u53ef\u9009\uff0c\u63d0\u53d6\u989c\u8272\u5206\u91cf\uff08rgb\uff09\u5e76\u5b58\u50a8\u4e8e\u5355\u72ec\u7684\u6570\u7ec4\u4e2d\u3002 \u5982\u4f55\u5c06\u56fe\u7247\u8f6c\u6362\u4e3a Java \u4e2d\u7684\u6570\u7ec4 \u5c06\u56fe\u7247\u8f6c\u6362\u4e3a Java \u4e2d\u7684\u6570\u7ec4\u7684\u65b9\u6cd5\u6709\u4ee5\u4e0b\u6b65\u9aa4\uff1a 1. \u8bfb\u53d6\u56fe\u7247 import java.awt.image.BufferedImage; import javax.imageio.ImageIO; \/\/ \u8bfb\u53d6\u56fe\u7247\u5230 BufferedImage \u5bf9\u8c61 BufferedImage image = ImageIO.read(new File(&#8220;image.png&#8221;)); \u767b\u5f55\u540e\u590d\u5236 2. \u63d0\u53d6\u50cf\u7d20 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \/\/ \u83b7\u53d6\u56fe\u7247\u5bbd\u5ea6\u548c\u9ad8\u5ea6 int width = image.getWidth(); int height = image.getHeight(); \/\/ \u521b\u5efa\u6570\u7ec4\u5b58\u50a8\u50cf\u7d20 int[] pixels = new int[width * height]; \/\/ \u904d\u5386\u6bcf\u4e2a\u50cf\u7d20\uff0c\u63d0\u53d6\u989c\u8272\u503c for [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-66553","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/66553","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=66553"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/66553\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=66553"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=66553"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=66553"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}