callback Function Classic Example
1. Using a Java callback function to implement a tool class to test the run time of a function
public class Testobject { /** * A method to be tested, a time-consuming loop is performed */public static void TestMethod () { int i= 0; i< 100000000; i++) { } } /** * A simple way to test the execution time of a method * /public void Testtime () { long begin = System.currenttimemillis (); Test start time testMethod ();//test method Long end = System.currenttimemillis ();//test End time System.out.println ("[ Use time]: "+ (End-begin)); Print use time } public static void Main (string[] args) { testobject test=new testobject (); Test.testtime (); } }
You see the Testtime () method, only "//test method" is needed to change, let's do a function to achieve the same function but more flexible:
2. First set a callback interface
Public interface CallBack { //method for performing callback operations void execute ();
3. Defining Tool Classes
public class Tools { /** * Test function usage time by defining the Execute method of the CallBack interface * @param callBack * /public void Testtime (CallBack CallBack) { Long begin = System.currenttimemillis ();///Test start time callback.execute ();///callback operation Long end = System.currenttimemillis ();//test End time System.out.println ("[Use time]:" + (End-begin)); Print usage Time }
?
?4. Test method
public static void Main (string[] args) { tools tool = new Tools (); Tool.testtime (New CallBack () { ///define Execute method public void execute () { //Here you can add one or more methods to test the run-time Testobject.testmethod ();}} );}
use of callbacks in projects
Requirements Description:
After customizing the camera to take a photo, use the callback in the Picturecallback that handles the image data
Objective:
Requires image generation when processing is complete (e.g., mass, size, proportional compression), using JNI to extract image features in callbacks, access to the network to achieve the function of image recognition
1. Declaring an interface
Package app.ui.callback;/** * @author Gao_chun * */public interface Mycallback {public void camerahasopened (); Camera public void Todistinguish (); Recognition
Picturecallback in the 2.CameraInterface class , providing a callback set method
public class Camerainterface {private static final String TAG = "Gao_chun"; public Boolean iscamerastop = false; Marks whether the camera turns on or off private camera mcamera; Private Camera.parameters Mparams; Private Boolean ispreviewing = false; private float mpreviwrate = -1f; Identify the callback private mycallback mcallback; public void Setmcallback (Mycallback mcallback) {this.mcallback = Mcallback; }/** * Photo * * * public void dotakepicture () {if (ispreviewing && (Mcamera! = null)) { Mcamera.takepicture (Mshuttercallback, NULL, mjpegpicturecallback); }}//callback for JPEG image data, one of the most important callbacks picturecallback Mjpegpicturecallback = new Picturecallback () {public void Onpicturetaken (byte[] data, camera camera) {LOG.I (TAG, "Myjpegcallback:onpicturetaken ..."); Bitmap mbitmap = null; if (null! = data) {//data is byte data, resolves it to bitmap mbitmap = Bitmapfactory.decodebytearray (data, 0, Data.length); Mcamera.stoppreview (); Ispreviewing = false; } if (null! = Mbitmap) {Bitmap Newbitmap = Fileoperation.scaletobitmap (mbitmap,fileop Eration.scale); According to bitmap picture width height, calculate/*int w = mbitmap.getwidth () proportionally; int h = mbitmap.getheight (); int scale = 512; Default scale int max = w > h? W:h; float R = scale * 1.0F/MAX; int ww = (int) Math.ceil (w * r); int hh = (int) Math.ceil (h * r); Bitmap Mbitmap = fileoperation.zoomimg (ROTABITMAP,WW,HH); *///fileoperation.delfile (fileoperation.filename_ ICON);//delete picture Fileoperation.savebitmap (Newbitmap,fileoperation.filename_icon);//Save Picture//callback method, need The action to be performed mcallback.todistinguish (); Mbitmap = null; Newbitmap = null; }//Enter preview again Mcamera.startpreview (); Ispreviewing = true; } };}
The 3.CameraActivity class implements the interface and method
public class Cameraactivity extends Titleactivity implements Mycallback {/* (non-javadoc) * @see App.ui.widget.TitleA Ctivity#onclick (Android.view.View) * Click to capture image and callback processing */@Override public void OnClick (view v) {Super.onclick (v); Switch (V.getid ()) {case r.id.shutter:camerainterface.getinstance (). Dotakepicture ();//Photo Judge SDcard in the signature file if (! Fileoperation.checkfileexist (FILEOPERATION.FILENAME_AVG) | | ! Fileoperation.checkfileexist (Fileoperation.filename_vec)) {//Read file write in assets directory SDcard Fileoperation.readassetsandwrite (This, "avg.txt"); Fileoperation.readassetsandwrite (This, "vec.txt"); } mloading.show (); Set the recognition callback Camerainterface.getinstance (). Setmcallback (this); Do not click Shutterview.setclickable again (FALSE); Break Default Break } */* (non-javadoc) * @see app.ui.callback.mycallback#todistinguish () * Image recognition */@Override public voi D todistinguish () {//TODO auto-generated Method stub if (Fileoperation.checkfileexist (Fileoperation.filenam E_icon)) {getpicturefeature ();//extract image features}else {dialogutils.showtoast (cameraactivity.this, "image Not complete, please re-shoot "); }}/** * 1. Get image path and signature file * 2. Extract features using JNI * 3. Access server */private void Getpicturefeature () {//Read image and lift Take characteristic parameter Mpicpath = Fileoperation.getfilepath () + Fileoperation.filename_icon; Mvecpath = Fileoperation.getfilepath () + Fileoperation.filename_vec; Mavgpath = Fileoperation.getfilepath () + fileoperation.filename_avg; LOG.I (TAG, "Mpicpath------------->" +mpicpath+ "\ n mvecpath---->" +mvecpath+ "\ n mavgpath---->" +mavgpath "); New Thread () {public void run () {Useopensift obj = new Useopensift (); Mtempdata = Obj.getfeature (Mpicpath,mvecpath,mavgpath); Imagerecognition (Mtempdata); Submit data}}.start (); }}
?
Android (Java) callback function classic Example