android - Notification when a specific color from a pixel is selected -
i want create program this:
- take picture;
- select area color;
- the app inform user current shade of white , select shade based on options receive app
what i'm trying message see if selected color (from touched position) closer shade of white i'm implementing (let's say, if touched color between #fcffffff , #fcfef9d5 program display "a2").
this code far:
public class mainactivity extends activity { textview touchedxy, invertedxy, imgsize, colorrgb; imageview imgsource1; button b, bopenfromgallery; static final int camrequest = 1; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); touchedxy = (textview) findviewbyid(r.id.xy); invertedxy = (textview) findviewbyid(r.id.invertedxy); imgsize = (textview) findviewbyid(r.id.size); colorrgb = (textview) findviewbyid(r.id.colorrgb); b = (button)findviewbyid(r.id.button01); bopenfromgallery = (button)findviewbyid(r.id.button_openfromgallery); imgsource1 = (imageview)findviewbyid(r.id.source1); b.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent cameraintent = new intent(android.provider.mediastore.action_image_capture); startactivityforresult(cameraintent, camrequest); } }); } protected void onactivityresult(int requestcode, int resultcode, intent data) { if (resultcode == result_ok) { if (requestcode == camrequest) { bitmap bitmap = (bitmap) data.getextras().get("data"); bitmap newbitmap = bitmap_resizer(bitmap, 1280, 768); imgsource1.setimagebitmap(newbitmap); imgsource1.setontouchlistener(imgsourceontouchlistener); } } } ontouchlistener imgsourceontouchlistener = new ontouchlistener() { @override public boolean ontouch(view view, motionevent event) { float eventx = event.getx(); float eventy = event.gety(); float[] eventxy = new float[]{eventx, eventy}; matrix invertmatrix = new matrix(); ((imageview) view).getimagematrix().invert(invertmatrix); invertmatrix.mappoints(eventxy); int x = integer.valueof((int) eventxy[0]); int y = integer.valueof((int) eventxy[1]); touchedxy.settext( "touched position: " + string.valueof(eventx) + " / " + string.valueof(eventy)); invertedxy.settext( "touched position: " + string.valueof(x) + " / " + string.valueof(y)); drawable imgdrawable = ((imageview) view).getdrawable(); bitmap bitmap = ((bitmapdrawable) imgdrawable).getbitmap(); imgsize.settext( "drawable size: " + string.valueof(bitmap.getwidth()) + " / " + string.valueof(bitmap.getheight())); //limit x, y range within bitmap if (x < 0) { x = 0; } else if (x > (bitmap.getwidth() - 1)) { x = bitmap.getwidth() - 1; } if (y < 0) { y = 0; } else if (y > (bitmap.getheight() - 1)) { y = bitmap.getheight() - 1; } int touchedrgb = bitmap.getpixel(x, y); colorrgb.settext("touched color: " + "#" + integer.tohexstring(touchedrgb)); colorrgb.settextcolor(touchedrgb); return true; } }; public bitmap bitmap_resizer(bitmap bitmap,int newwidth,int newheight) { bitmap scaledbitmap = bitmap.createscaledbitmap(bitmap, newwidth, newheight, true); float ratiox = newwidth / (float) bitmap.getwidth(); float ratioy = newheight / (float) bitmap.getheight(); float middlex = newwidth / 2.0f; float middley = newheight / 2.0f; matrix scalematrix = new matrix(); scalematrix.setscale(ratiox, ratioy, middlex, middley); canvas canvas = new canvas(scaledbitmap); canvas.setmatrix(scalematrix); canvas.drawbitmap(bitmap, middlex - bitmap.getwidth() / 2, middley - bitmap.getheight() / 2, new paint(paint.filter_bitmap_flag)); return scaledbitmap; }
}
as here said, can image top-left corner, calculate touched position. after that, u can color of area doing this:
bitmap bitmap = ((bitmapdrawable)imageview.getdrawable()).getbitmap(); int pixel = bitmap.getpixel(x,y);
you can filter color this:
int red = color.red(pixel); int blue = color.blue(pixel); int green = color.green(pixel);
Comments
Post a Comment