Convert Image to Grayscale with array matrix RGB in android -
i'm creating code barre reader , have taken rgb picture , store in array using onpicturetaken(byte[] _data, camera _camera) , need convert binary matrix (greyscale) have done far
public class cam extends activity implements callback { camera camera = null; @suppresswarnings("deprecation") @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); requestwindowfeature(window.feature_no_title); surfaceview view = new surfaceview(this); view.getholder().addcallback(this); view.getholder().settype(surfaceholder.surface_type_push_buffers); setcontentview(view); } @override public void surfacechanged(surfaceholder holder, int format, int width, int height) { camera.parameters parameters = camera.getparameters(); parameters.setpreviewsize(width, height); parameters.getsupportedpictureformats(); list<integer> formats = parameters.getsupportedpictureformats(); if (formats.contains(pixelformat.rgb_888)) parameters.setpictureformat(pixelformat.rgb_888); camera.setparameters(parameters); camera.startpreview(); } @override public void surfacecreated(surfaceholder holder) { try { camera = camera.open(); camera.setpreviewdisplay(holder); } catch (exception e) { e.printstacktrace(); } } @override public void surfacedestroyed(surfaceholder holder) { camera.stoppreview(); camera.release(); camera = null; } @suppresslint("showtoast") @override public boolean ontouchevent(motionevent event) { switch(event.getaction()){ case motionevent.action_down: picturecallback jpegcallback = new picturecallback() { public void onpicturetaken(byte[] _data, camera _camera) { try { file fs = new file(cam.this.getfilesdir()+"/image.jpeg"); fileoutputstream fos = new fileoutputstream(fs); fos.write(_data); fos.flush(); fos.close(); toast.maketext(cam.this,fs.getabsolutepath(),0).show(); } catch (exception e) { e.printstacktrace(); } camera.startpreview(); } }; camera.takepicture(null, null, jpegcallback); break; } return super.ontouchevent(event); }
}
make bitmap
out of , do
private byte[] converttogray(bitmap bmp) { int width = bmp.getwidth(); int height = bmp.getheight(); byte[] grayscale = new byte[width*height]; int val, r, g, b; (int = 0; < height; i++) { (int j = 0; j < width; j++) { val = bmp.getpixel(j, i); r = (val >> 16) & 0xff ; g = (val >> 8) & 0xff ; b = val & 0xff ; grayscale[i*width + j] = (byte)(0.21*r + 0.71*g + 0.07*b); } } return grayscale;
cheers!
Comments
Post a Comment