I. Bitmap INTRODUCTION 1. Image Usage Scenarios
1) Effect class
2) Non-visible component image comparison
3) Failures and exceptions
4) Using the image to judge the component
2. Some API simple description
|
Api |
Description |
| Compress |
Compress pictures |
| Copy |
Copy Picture |
| CreateBitmap |
Create a picture |
| GetHeight |
Get Picture height |
| GetWidth |
Get Picture width |
| GetPixel |
Get a point color value |
| SetPixel |
Set a point color value |
3. Create a Bitmap instance
//Method Body Code Public classImagetestcaseextendsuiautomatortestcase{ Public voidSavebitmaptosdcard (Bitmap bitmap,string newName) {FileOutputStream out=NULL; Try{ out=NewFileOutputStream ("/mnt/sdcard/" +newname+ ". jpg"); if(out!=NULL){ //three parameters are format, saved file quality, file streamBitmap.compress (Bitmap.CompressFormat.JPEG, 90, out); Out.close (); } } Catch(Exception e) {Throw NewRuntimeException (e); } }}//use case part code Public classTest1extendsimagetestcase{//Quick Commissioning Public Static voidmain (String [] args) {NewUiautomatorhelper ("Test", "Testdemo1.test1", "TestDemo1", "2"); } //Create a bitmap Public voidTestDemo1 () {//A. Capturing a pictureString path= "/mnt/sdcard/testbitmap.png"; File Storepath=NewFile (Path); Uidevice.getinstance (). Takescreenshot (Storepath); Sleep (1000); //B. Rename and save the picture//Create a bitmap from a fileBitmap bitmap=Bitmapfactory.decodefile (Path); //Call method BodySavebitmaptosdcard (Bitmap, "new-image-88"); }}Second, get the pixel value and image interception
Pixel value: The color value of each pixel point
1. Get a point pixel value instance
//Method Body Code Public classImagetestcaseextendsuiautomatortestcase{//Get components by description PublicUiObject obj (String text) {return NewUiObject (Newuiselector (). Description (text)); } //save after capturing a picture Public voidcutbitmap (Rect rect,string path) {Bitmap m=bitmapfactory.decodefile (path); M=M.createbitmap (M,rect.left,rect.top,rect.width (), Rect.height ()); //call the method in the example above, the actual debugging process if necessary to add the method bodySavebitmaptosdcard (M, "cutimg_88"); } //get the color value of a point Public intGetcolorpicel (intXinty) {String path= "/mnt/sdcard/testcolor.png"; File File=NewFile (path); Uidevice.getinstance (). takescreenshot (file); Bitmap m=bitmapfactory.decodefile (path); intColor=m.getpixel (x, y); returncolor; }}//use case Code Public classTest1extendsimagetestcase{//Quick Commissioning Public Static voidmain (String [] args) {NewUiautomatorhelper ("Test", "Testdemo1.test1", "TestDemo2", "2"); } //Use Cases Public voidTestDemo2 ()throwsuiobjectnotfoundexception{//Capturing PicturesRect rect = obj ("City"). getbounds (); String Path= "/mnt/sdcard/testcolor.png"; File File=NewFile (path); Uidevice.getinstance (). takescreenshot (file); //Call method BodyCutbitmap (Rect,path); //call method body to get the color value of a point intColor=Getcolorpicel (Rect.centerx (), Rect.centery ()); System.out.println ("COLOR:" +color); }}Third, the image embedded text
When you want to use the use case to write text on the image, easy to see quickly
1. Image Embedding Text Example:
//method Body Public classImagetestcaseextendsuiautomatortestcase{//Method Public voidScreenshotanddrawrext (String path,string imagename,string text) {File file=NewFile (path); Uidevice.getinstance (). takescreenshot (file); Bitmap Bitmap=bitmapfactory.decodefile (path); Bitmap Drawbitmap=Drawtextbitmap (Bitmap,text); Savebitmaptosdcard (Drawbitmap, imageName);//call the method in the first example above } //Embed text methods PublicBitmap drawtextbitmap (Bitmap bitmap,string text) {intx=bitmap.getwidth (); inty=bitmap.getheight (); //Create a larger bitmapBitmap Newbitmap=bitmap.createbitmap (x,y+80, Bitmap.Config.ARGB_8888); Canvas Canvans=NewCanvas (NEWBITMAP); Paint Paint=NewPaint (); //overlay a picture in the original position (0,0)Canvans.drawbitmap (bitmap, 0, 0, paint); //Brush WinkPaint.setcolor (Color.parsecolor ("#FF0000")); Paint.settextsize (80);//Set Text SizeCanvans.drawtext (text, y+55, paint);//WriteCanvans.save (Canvas.all_save_flag);//SaveCanvans.restore (); returnNewbitmap; }}//Use Cases Public classTest1extendsimagetestcase{//Quick Commissioning Public Static voidmain (String [] args) {NewUiautomatorhelper ("Test", "Testdemo1.test1", "TestDemo3", "2"); } Public voidTestDemo3 () {String path= "/mnt/sdcard/testdrawtext.png"; String ImageName= "testdrawtext_888"; String text= "Test Input"; //Call method Bodyscreenshotanddrawrext (path, imageName, text); } }Four, image contrast
Some special components cannot obtain the component information and cannot determine the status
1. Image Comparison Example
//method Body Public classImagetestcaseextendsuiautomatortestcase{//the method of image contrast Public BooleanImagesameas (String targetimagepath,string Compath,Doublepercent) { Try { //creation of two bitmapBitmap m1=Bitmapfactory.decodefile (Targetimagepath); Bitmap m2=Bitmapfactory.decodefile (Compath); //declaring Variables intWidth=m2.getwidth (); intheight=m2.getheight (); intNumdiffpixels=0; //Cross-sectional comparison involving two loops for(inty=0;y){ for(intx=0;x<width;x++){ //to take an unequal pixel value if(M2.getpixel (x, y)! =m1.getpixel (x, y)) {Numdiffpixels++; } } } DoubleTotalpices=height*width;//Total pixel value DoubleDiffpercent=numdiffpixels/totalpices;//Percentage of unequal returnpercent<=1.0-diffpercent;//returns the degree of similarity}Catch(Exception e) {}return false; }}//Picture Comparison use case section Public classTest1extendsimagetestcase{//Quick Commissioning Public Static voidmain (String [] args) {NewUiautomatorhelper ("Test", "Testdemo1.test1", "TestDemo4", "2"); } //Picture Comparison Public voidTestDemo4 () {//capture two contrasting graphsString targetimagepath= "/mnt/sdcard/c1.png"; String Compath= "/mnt/sdcard/c2.png"; File F1=NewFile (Targetimagepath); File F2=NewFile (Compath); Uidevice.getinstance (). Takescreenshot (F1); Sleep (1000); Uidevice.getinstance (). Presshome ();//a different scene .Sleep (1000); Uidevice.getinstance (). Takescreenshot (F2); //Call Image Comparison method BooleanB=imagesameas (targetimagepath,compath,1.0d); //Output Comparison ResultsSystem.out.println ("Image comparison result:" +b); }}
14.Android uiautomator Image Processing