Android - Problems cropping a Bitmap to display in an ImageView -
thanks reading, have been trying going days , have searched code can. see below have tried number of approaches.
i using 3rd party device returns bitmap of size 152 x 500. there black strip vertically down left side 24 wide. in actual fact needing 128 x 500 start 24 in left.
whatever try cannot seem rid of black strip when image saved.
here code, sorry it's thought show all.
// scan button clicked , log.e("tag", "wait scan capture");
much appreciate on this.
public class imagecaptureactivity extends activity implements onclicklistener { private linearlayout ll_root; private button btn_scan; private button btn_process; private linearlayout mlayout; private button mbutton; private fingerview mimageview; private fpcdevkitinterface mfpcdevkit; public view minutiaview; public bitmap tempbitmap; public bitmap scanbitmap; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.my_login_scan); log.e("tag", "0"); //ll_root = (linearlayout)findviewbyid(r.id.ll_root); btn_scan = (button)findviewbyid(r.id.scan); btn_process = (button)findviewbyid(r.id.proceed_button); log.e("tag", "1"); // mfpcdevkit = new ssddevice ((usbmanager) getsystemservice(context.usb_service)); // mimageview = new fingerview(this, mfpcdevkit.createcompatiblebitmap()); // mimageview.setlayoutparams(new linearlayout.layoutparams(200,200)); //add view here. // ll_root.addview(mimageview); // keep commented, because cannot find id of ll_root. null. log.i("tag", "2"); log.e("tag", "5"); /* ---------------------------------------------------------------------------------------------------------------*/ /* following initial screen layout section done setup of screen , before capture clicked */ // manually set screen layout mlayout = new linearlayout(this); mlayout.setorientation(linearlayout.vertical); // manually create button mbutton = new button(this); mbutton.setonclicklistener(this); mbutton.settext("capture image"); // setup fpc service finger scan device log.e("tag", "setup fpc"); mfpcdevkit = new ssddevice ((usbmanager) getsystemservice(context.usb_service)); // call fingerview function , load bitmap mimageview log.e("tag", "load mimageview"); mimageview = new fingerview(this, mfpcdevkit.createcompatiblebitmap()); // modify viewing area scanned image size mimageview.setlayoutparams(new linearlayout.layoutparams(24+128,500)); // left area plus image width , crop of right area // add button , mimageview layout log.e("tag", "add views"); mlayout.addview(mbutton); mlayout.addview(mimageview); // display screen log.e("tag", "display screen"); setcontentview(mlayout); // setcontentview(r.layout.my_login_scan); /* ----------- end screen layout setup ------------------ */ } public static alertdialog createcapturepromtdialog(context context, devkittype type) { alertdialog.builder builder = new alertdialog.builder(context); if (type == devkittype.asd) builder.settitle("place finger on sensor"); else builder.settitle("swipe finger on sensor"); return builder.create(); } private class fingerview extends view { public bitmap mbitmap; private matrix mmatrix; public fingerview(context context, bitmap bitmap) { super(context); mbitmap = bitmap; mmatrix = new matrix(); setbackgroundcolor(color.white); // full screen area background colour template area colour setdrawingcacheenabled(true); log.e("tag", "fingerview"); } public bitmap getbitmap() { return mbitmap; } @override protected void onsizechanged(int w, int h, int oldw, int oldh) { log.e("onsizechanged", "w = " + w + " h = " + h + " oldw = " + oldw + " oldh = " + oldh); // use scale current bitmap fill screen. // last variable: matrix.scaletofit.fill fill 2 dimensions // matrix.scaletofit.center scale center , keep // width/height ratio. //mmatrix.setrecttorect( // new rectf(0, 0, mbitmap.getwidth(), mbitmap.getheight()), // new rectf(0, 0, w, h), matrix.scaletofit.center); super.onsizechanged(w, h, oldw, oldh); // lets try resize 150 x 500 - harry july 14 2013 // mmatrix.setrecttorect(new rectf(0, 0, 128, 500),new rectf(-24, 0, 150, 500), matrix.scaletofit.center); log.e("tag", "inside onsizechanged " + 0); /* scales image fit new rectangle imageview matrix m = imageview.getimagematrix(); rectf drawablerect = new rectf(0, 0, imagewidth, imageheight); rectf viewrect = new rectf(0, 0, imageview.getwidth(), imageview.getheight()); m.setrecttorect(drawablerect, viewrect, matrix.scaletofit.center); imageview.setimagematrix(m); */ } @suppresslint("drawallocation") @override protected void ondraw(canvas canvas) { log.e("ondraw begin",""); canvas.drawbitmap(mbitmap, mmatrix, null); super.ondraw(canvas); } } // scan button clicked public void onclick(view v) { log.e("tag", "onclick happened"); // erase scan area white mimageview.getbitmap().erasecolor(color.white); // doesn't seem make difference // bitmap tempbitmap = bitmap.createbitmap(128+24+24,500,bitmap.config.argb_8888); log.e("onclick enter second one= " + 0, "w = " + 0 + " h =" + 0); try { // wait scanner , load image mimageview.mbitmap mfpcdevkit.clearabort(); log.e("tag", "wait scan capture"); scanbitmap = mfpcdevkit.createcompatiblebitmap(); mfpcdevkit.waitandcaptureimage(mimageview.getbitmap()); // image displays in mimageview //mfpcdevkit.waitandcaptureimage(scanbitmap); // tried put image bitmap //create new image bitmap , attach brand new canvas // bitmap minbitmap = bitmap.createbitmap(mybitmap.getwidth(), mybitmap.getheight(), bitmap.config.argb_8888); canvas tempcanvas = new canvas(scanbitmap); //draw image bitmap canvas // tempcanvas.drawbitmap(scanbitmap, 0, 0, null); //draw else want canvas, in example rectangle rounded edges // tempcanvas.drawroundrect(new rectf(x1,y1,x2,y2), 2, 2, scanbitmap); //attach canvas imageview // mimageview.setbackgrounddrawable(new bitmapdrawable(getresources(), scanbitmap)); // @override protected void ondraw(canvas canvas) { canvas.drawcolor(0xffaaaaaa); canvas.drawbitmap(mbitmap, 0, 0, mbitmappaint); if(item_id==3){ canvas.drawtext(appmodel.contents, 100, 100, mpaint); } else{ canvas.drawpath(mpath, mpaint); } } // bitmap mmbitmap = bitmap.createbitmap(mimageview.mbitmap, 22, 0, mimageview.mbitmap.getwidth()-100, mimageview.mbitmap.getheight()); // canvas canvas1 = new canvas(mmbitmap); // mimageview.setimagebitmap(tempbitmap); // crop image here. log.e("before crop", "w = " + mimageview.getbitmap().getwidth() + " h = " + mimageview.getbitmap().getheight()); // parameters show w = 176 h = 500 // harry's mods flow here -------- // bitmap harrybitmap = bitmap.createbitmap(mimageview.getwidth(),mimageview.getheight(),bitmap.config.argb_8888); // bitmap harrybitmap = bitmap.createbitmap(128,500,bitmap.config.argb_8888); // canvas canvas = new canvas(harrybitmap); // mimageview.draw(canvas); // bitmap resizedbitmap1 = bitmap.createbitmap(tempbitmap,24,0,150,500); // canvas canvas1 = new canvas(resizedbitmap1); // mimageview.draw(canvas1); /* try number 2 approach paint paint = new paint(); paint.setfilterbitmap(true); int targetwidth = 128; int targetheight = 500; bitmap targetbitmap = bitmap.createbitmap(targetwidth, targetheight,bitmap.config.argb_8888); canvas canvas = new canvas(scanbitmap); canvas.drawbitmap( scanbitmap, new rect(24, 0, 176, 500), new rectf(0, 0, targetwidth, targetheight), paint); // matrix matrix = new matrix(); // matrix.postscale(1f, 1f); // bitmap resizedbitmap = bitmap.createbitmap(targetbitmap, 0, 0, 128, 500, matrix, true); // // //convert bitmap resource // //bitmapdrawable bd = new bitmapdrawable(resizedbitmap); // // canvas.drawbitmap( resizedbitmap, new rect(24, 0, 128, 500), // new rect(0, 0, 128, 500), paint); mimageview.draw(canvas); */ log.e("after crop", "w = " + mimageview.getbitmap().getwidth() + " h = " + mimageview.getbitmap().getheight()); // end harry's mods flow -------- // save image sd card testing viewing savescreen(mimageview.getbitmap()); } catch (ioexception e) { toast.maketext(this, e.tostring(), toast.length_long).show(); e.printstacktrace(); } catch (interruptedexception e) { // todo auto-generated catch block e.printstacktrace(); } mimageview.postinvalidate(); } // not used - harry july 14 2013 private bitmap cropbitmap(bitmap bitmap) { bitmap result = bitmap.createbitmap(bitmap, 1, 1, 128, 500); log.e("croped", "w = " + result.getwidth() + " h = " + result.getheight()); return result; } public void savescreen(bitmap bitmap) { log.e("savescreen enter= " + bitmap.getbytecount(), "w = " + bitmap.getwidth() + " h =" + bitmap.getheight()); string root = environment.getexternalstoragepublicdirectory( environment.directory_pictures).tostring(); file mydir = new file(root + "/images"); mydir.mkdirs(); string fname = "fpckit.jpg"; file file = new file(mydir, fname); /* * boolean done = false; int n = 1; { string fname = "image-" + n + * ".jpg"; file = new file(mydir, fname); if (!file.exists()) { done = * true; } n++; } while (!done); */ try { fileoutputstream out = new fileoutputstream(file); // bitmap.compress(bitmap.compressformat.jpeg, 100, out); // bitmap bmp = mimageview.getbitmap(); // bmp.compress(bitmap.compressformat.jpeg, 100, out); log.e("onsave bytes= " + bitmap.getbytecount(), "w = " + bitmap.getwidth() + " h =" + bitmap.getheight()); //this previous version. save entire image. mimageview.getdrawingcache().compress(bitmap.compressformat.jpeg, 100, out); // bitmap.compress(bitmap.compressformat.png, 100, out); out.flush(); out.close(); toast toast = toast.maketext(imagecaptureactivity.this, "image saved " + file.getabsolutepath(), toast.length_short); toast.show(); } catch (exception e) { e.printstacktrace(); toast toast = toast.maketext(imagecaptureactivity.this, "exception saving image: " + e.getlocalizedmessage(), toast.length_short); toast.show(); } } }
Comments
Post a Comment