在Android层实现触摸屏校验

Android 触摸屏校准

--by ONCE

 这个类是对ts calibrate里的函数的改写 但是其实仅仅这个是不行的。package com.android.calibrate;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import android.util.Log;public class Calibrate {    private calibration cal;    public Calibrate() {        cal = new calibration();    }    class calibration {        int x[] = new int[5];        int xfb[] = new int[5];        int y[] = new int[5];        int yfb[] = new int[5];        int a[] = new int[7];    };    boolean perform_calibration() {        Log.i("XXW", "perform_calibration");        int j;        float n, x, y, x2, y2, xy, z, zx, zy;        float det, a, b, c, e, f, i;        float scaling = (float)65536.0;        // Get sums for matrix        n = x = y = x2 = y2 = xy = 0;        for (j = 0; j < 5; j++) {            n += 1.0;            x += (float)cal.x[j];            y += (float)cal.y[j];            x2 += (float)(cal.x[j] * cal.x[j]);            y2 += (float)(cal.y[j] * cal.y[j]);            xy += (float)(cal.x[j] * cal.y[j]);        }        // Get determinant of matrix -- check if determinant is too small        det = n * (x2 * y2 - xy * xy) + x * (xy * y - x * y2) + y * (x * xy - y * x2);        if (det < 0.1 && det > -0.1) {            Log.i("ts_calibrate: determinant is too small -- %f/n", "" + det);            return false;        }        // Get elements of inverse matrix        a = (x2 * y2 - xy * xy) / det;        b = (xy * y - x * y2) / det;        c = (x * xy - y * x2) / det;        e = (n * y2 - y * y) / det;        f = (x * y - n * xy) / det;        i = (n * x2 - x * x) / det;        // Get sums for x calibration        z = zx = zy = 0;        for (j = 0; j < 5; j++) {            z += (float)cal.xfb[j];            zx += (float)(cal.xfb[j] * cal.x[j]);            zy += (float)(cal.xfb[j] * cal.y[j]);        }        // Now multiply out to get the calibration for framebuffer x coord        cal.a[0] = (int)((a * z + b * zx + c * zy) * (scaling));        cal.a[1] = (int)((b * z + e * zx + f * zy) * (scaling));        cal.a[2] = (int)((c * z + f * zx + i * zy) * (scaling));        System.out.printf("%f %f %f/n", (a * z + b * zx + c * zy), (b * z + e * zx + f * zy), (c                * z + f * zx + i * zy));        // Get sums for y calibration        z = zx = zy = 0;        for (j = 0; j < 5; j++) {            z += (float)cal.yfb[j];            zx += (float)(cal.yfb[j] * cal.x[j]);            zy += (float)(cal.yfb[j] * cal.y[j]);        }        // Now multiply out to get the calibration for framebuffer y coord        cal.a[3] = (int)((a * z + b * zx + c * zy) * (scaling));        cal.a[4] = (int)((b * z + e * zx + f * zy) * (scaling));        cal.a[5] = (int)((c * z + f * zx + i * zy) * (scaling));        System.out.printf("%f %f %f/n", (a * z + b * zx + c * zy), (b * z + e * zx + f * zy), (c                * z + f * zx + i * zy));        // If we got here, we're OK, so assign scaling to a[6] and return        cal.a[6] = (int)scaling;        return true;        /*         * // This code was here originally to just insert default values         * for(j=0;j<7;j++) { c->a[j]=0; } c->a[1] = c->a[5] = c->a[6] = 1;         * return 1;         */    }    void get_sample(int index, int x1, int y1, int x, int y) {        Log.i("XXW", "get_sample");        cal.x[index] = x1;        cal.y[index] = y1;        cal.xfb[index] = x;        cal.yfb[index] = y;    }    int calibrate_main() {        int result = 0;        Log.i("XXW", "calibrate_main");        if (perform_calibration()) {            String strPara = String.format("%d %d %d %d %d %d %d", cal.a[1], cal.a[2], cal.a[0],                    cal.a[4], cal.a[5], cal.a[3], cal.a[6]);            boolean success = new File("/data/etc").mkdir();            if (!success) {                Log.i(this.toString(), "no success");            }            File desFile = new File("/data/etc/pointercal");            if (!desFile.exists())                try {                    desFile.createNewFile();                } catch (IOException e1) {                    e1.printStackTrace();                }            FileOutputStream fos;            try {                fos = new FileOutputStream(desFile);                byte[] buf = strPara.getBytes();                int bytesRead = buf.length;                try {                    fos.write(buf, 0, bytesRead);                    fos.flush();                    fos.close();                } catch (IOException e) {                                        e.printStackTrace();                }            } catch (FileNotFoundException e) {                              e.printStackTrace();            }            result = 0;        } else {            result = -1;        }        return result;    }}

 package com.android.calibrate;import com.android.calibrate.R;import android.app.Activity;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.os.Bundle;import android.util.Log;import android.view.MotionEvent;import android.view.View;import android.view.Window;import android.view.WindowManager;import android.view.View.OnTouchListener;import android.widget.Toast;import android.os.SystemProperties;public class AndroidCalibrate extends Activity {    final String TAG = "ScreenCalibration";    final int UI_SCREEN_WIDTH = 800;    final int UI_SCREEN_HEIGHT = 480;    CrossView myview;    int direction;    private Calibrate cal;    int xList[] = {            50, UI_SCREEN_WIDTH - 50, UI_SCREEN_WIDTH - 50, 50, UI_SCREEN_WIDTH / 2    };    int yList[] = {            50, UI_SCREEN_HEIGHT - 50, UI_SCREEN_HEIGHT - 50, 50, UI_SCREEN_HEIGHT / 2    };    static void setNotTitle(Activity act) {        act.requestWindowFeature(Window.FEATURE_NO_TITLE);    }    static void setFullScreen(Activity act) {        setNotTitle(act);        act.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,                WindowManager.LayoutParams.FLAG_FULLSCREEN);    }    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setFullScreen(this);            myview = new CrossView(this);        setContentView(myview);                SystemProperties.set("ts.config.calibrate", "start");                cal = new Calibrate();        direction = 0;                myview.setOnTouchListener(new OnTouchListener() {            @Override            public boolean onTouch(View v, MotionEvent event) {                Log.i("OnTouch", event.getX() + "," + event.getY());                v.invalidate();                if (direction < 4) {                    Log.i("XXW time onTouchListener", " " + direction);                    cal.get_sample(direction, (int)event.getX(), (int)event.getY(),                            xList[direction], yList[direction]);                }                if (direction == 4) {                    cal.get_sample(direction, (int)event.getX(), (int)event.getY(),                            xList[direction], yList[direction]);                    Log.i("XXW", "calibrate_main");                    cal.calibrate_main();                    Toast.makeText(getBaseContext(), "Calibrate Done!", Toast.LENGTH_SHORT).show();                    SystemProperties.set("ts.config.calibrate", "done");                    AndroidCalibrate.this.finish();                }                direction++;                return false;            }        });    }    public class CrossView extends View {        public CrossView(Context context) {            super(context);        }        public void onDraw(Canvas canvas) {            Paint paint = new Paint();            paint.setColor(Color.GREEN);            if (direction == 0) {                canvas.drawLine(40, 50, 60, 50, paint);                canvas.drawLine(50, 40, 50, 60, paint);                paint.setColor(Color.WHITE);            } else if (direction == 1) {                canvas.drawLine(UI_SCREEN_WIDTH - 60, 50, UI_SCREEN_WIDTH - 40, 50, paint);                canvas.drawLine(UI_SCREEN_WIDTH - 50, 40, UI_SCREEN_WIDTH - 50, 60, paint);                paint.setColor(Color.WHITE);            } else if (direction == 2) {                canvas.drawLine(UI_SCREEN_WIDTH - 60, UI_SCREEN_HEIGHT - 50, UI_SCREEN_WIDTH - 40,                        UI_SCREEN_HEIGHT - 50, paint);                canvas.drawLine(UI_SCREEN_WIDTH - 50, UI_SCREEN_HEIGHT - 60, UI_SCREEN_WIDTH - 50,                        UI_SCREEN_HEIGHT - 40, paint);                paint.setColor(Color.WHITE);            } else if (direction == 3) {                canvas.drawLine(40, UI_SCREEN_HEIGHT - 50, 60, UI_SCREEN_HEIGHT - 50, paint);                canvas.drawLine(50, UI_SCREEN_HEIGHT - 60, 50, UI_SCREEN_HEIGHT - 40, paint);                paint.setColor(Color.WHITE);            } else if (direction == 4) {                canvas.drawLine(UI_SCREEN_WIDTH / 2 - 10, UI_SCREEN_HEIGHT / 2,                        UI_SCREEN_WIDTH / 2 + 10, UI_SCREEN_HEIGHT / 2, paint);                canvas.drawLine(UI_SCREEN_WIDTH / 2, UI_SCREEN_HEIGHT / 2 - 10,                        UI_SCREEN_WIDTH / 2, UI_SCREEN_HEIGHT / 2 + 10, paint);                paint.setColor(Color.WHITE);            } else {            }            // canvas.drawText(getResources().getString(R.string.            // screen_calibration_content),            // UI_SCREEN_WIDTH / 2 - 50, UI_SCREEN_HEIGHT / 2, paint);            super.onDraw(canvas);        }    }}综上所述 这篇文章只是一篇错了很久的文章。 不过最近可能会有一块板子调试下 我会用jni写一个版本出来用用。 jni的代码早就写了一份了 试验一下如果是好的 我会更新这个文章。

#include "jni.h"
#include "JNIHelp.h"
#include <utils/Log.h>
#include "ts_calibrate.h"
#include <android_runtime/AndroidRuntime.h>

static calibration cal;

static jint calibrate(JNIEnv * env, jobject jobj)
{
LOGD("calibrate!/n");
return calibrate_main(cal);
}

static jint getScreenWidth(JNIEnv * env, jobject job)
{
LOGD("getXres!/n");
return getXres();
}

static jint getScreenHeight(JNIEnv * env, jobject job)
{
LOGD("getYres!/n");
return getYres();
}

static void getXY(JNIEnv * env, jobject job, jint index, int x1, int y1, int x, int y)
{
LOGD("getXY!/n");
get_sample(cal, index, x1, y1, x, y);
}

static const char *classPathName = "com/test/TestTsLib";

static JNINativeMethod methods[] = {
{"getScreenWidth", "()I", (void*)getScreenWidth },
{"getScreenHeight", "()I", (void*)getScreenHeight },
{"getXY", "(IIIII)V", (void*)getXY },
{"calibrate", "()I", (void*)calibrate },
};

/*
* Register several native methods for one class.
*/
static int registerNativeMethods(JNIEnv* env, const char* className,
JNINativeMethod* gMethods, int numMethods)
{
jclass clazz;

clazz = env->FindClass(className);
if (clazz == NULL) {
LOGE("Native registration unable to find class '%s'", className);
return JNI_FALSE;
}
if (env->RegisterNatives(clazz, gMethods, numMethods) < 0) {
LOGE("RegisterNatives failed for '%s'", className);
return JNI_FALSE;
}

return JNI_TRUE;
}

/*
* Register native methods for all classes we know about.
*
* returns JNI_TRUE on success.
*/
static int registerNatives(JNIEnv* env)
{
if (!registerNativeMethods(env, classPathName,
methods, sizeof(methods) / sizeof(methods[0]))) {
return JNI_FALSE;
}

return JNI_TRUE;
}

typedef union {
JNIEnv* env;
void* venv;
} UnionJNIEnvToVoid;

{
UnionJNIEnvToVoid uenv;
uenv.venv = NULL;
jint result = -1;
JNIEnv* env = NULL;

if (vm->GetEnv(&uenv.venv, JNI_VERSION_1_4) != JNI_OK) {
LOGE("ERROR: GetEnv failed");
goto bail;
}
env = uenv.env;

if (registerNatives(env) != JNI_TRUE) {
LOGE("ERROR: registerNatives failed");
goto bail;
}

result = JNI_VERSION_1_4;

bail:
return result;
}

• 本文已收录于以下专栏：

举报原因： 您举报文章：在Android层实现触摸屏校验 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)