Boa Noite Amigos!
Tenho já a dias pesquisado sobre tratamento de imagens na Internet mas os resultados não tem sido satisfatórios.
Fiz curso de Java e estou fazendo curso de Android, mas já pedi ajuda pro meu professor e ele falou que esse tópico não cai no curso e nenhum professor lá nunca tratou com imagens em android, algo que cheguei a conclusão que é um pouco complexo.
O meu problema é o seguinte: efetuar o tratamento de imagens no android, como por exemplo, aplicar um filtro de negativo, ou filtro de preto e branco.
Em Java consegui fazer sem problemas, armazenando a imagem em buffer e tratando ela. Consegui pq tive computação gráfica e como era Java tive uma boa orientação.
Segue código em Java:
// Abrir Imagem Colorida:
public static int[][][] abrirImagemColorida (String fileName) throws IOException {
Image image = ImageIO.read(new File( fileName));
BufferedImage bi = null;
if (image instanceof BufferedImage)
bi = (BufferedImage) image;
else {
int w = image.getWidth(null);
int h = image.getHeight(null);
bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g = bi.createGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();
}
int width = bi.getWidth();
int height = bi.getHeight();
int pixels[][][] = new int[width][height][3];
int rgb, r, g, b;
for (int w = 0; w < width; w++) {
for (int h = 0; h < height; h++) {
rgb = bi.getRGB(w, h);
r = (int) ((rgb & 0x00FF0000) >>> 16); // Red level
g = (int) ((rgb & 0x0000FF00) >>> 8); // Green level
b = (int) (rgb & 0x000000FF); // Blue level
pixels[w][h][0] = r;
pixels[w][h][1] = g;
pixels[w][h][2] = b;
}
}
return pixels;
}
// Filtro Negativo
public class Negativo
{
public static void main (String[]args) throws Exception
{
int img[][][] = ImagemUtil.abrirImagemColorida("C:\\arquivos1\\bugatti_veyron_4.jpeg");
int largura = img.length;
int altura = img[0].length;
int bandas = img[0][0].length;
int imgS[][][] = new int [largura][altura][bandas];
for (int x=0; x < largura; x++){
for (int y=0; y < altura; y++){
imgS[x][y][0]= 255 - img[x][y][0];
imgS[x][y][1]= 255 - img[x][y][1];
imgS[x][y][2]= 255 - img[x][y][2];
}
}
ImagemUtil.visualizarImagemColorida(img, "Entrada");
ImagemUtil.visualizarImagemColorida(imgS, "Saída");
ImagemUtil.salvarImagemColorida(imgS, "C:\\arquivos1\\bugatti_veyron_4_02.jpg");
}
Mas fazer o mesmo em Android tem sido complicado. Na Internet tem bastante fontes de pesquisa para Java, mas para Android poucas e meu inglês não tem me ajudado muito na minha consulta as classe Bitmap e Drawable do developer.android.
Pegar uma imagem até consigo:
//Uma imagem da Internet:
public class Tela1 extends Activity implements OnClickListener
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button botao = (Button) findViewById(R.id.mCarregarButton);
botao.setOnClickListener(this);
}
public void onClick(View view)
{
new Thread(new Runnable()
{
public void run()
{
final ImageView imgView = (ImageView) findViewById(R.id.mImagemImageView);
imgView.post(new Runnable()
{
public void run()
{
Drawable image = null;
try
{
image = getImagem();
}
catch(Exception e)
{
Log.i("LogErro: ", e.getMessage());
}
ImageView imgView = (ImageView) findViewById(R.id.mImagemImageView);
imgView.setImageDrawable(image);
}
});
}
}).start();
}
public Drawable getImagem() throws Exception
{
URL url = new URL("http://learning.com.br/Imagens/android.gif");
InputStream is = (InputStream) getObjeto (url);
Drawable d = Drawable.createFromStream(is,"src");
return d;
}
private Object getObjeto(URL url) throws MalformedURLException, IOException
{
Object content = url.getContent();
return content;
}
// Pegar imagem no cartao sd
String img = Environment.getExternalStorageDirectory().toString() + "/teste.jpg";
Bitmap btm = BitmapFactory.decodeFile(img);
Mas a maneira como adaptar para chegar a mesma conclusão do Java para o Android não consigo…
Alguém poderia me auxiliar se já tem experiência com Android?
Desde já grato pela ajuda!