我有一个名为grayVal的变量,它告诉我相机所指向的像素的灰度值。显然,当我四处移动相机时,此数字一直在变化。我的问题是,当我在带有循环的异步中传递grayVal来测试grayVal通过阈值需要多长时间时,无论我将相机指向什么,该数字都不会改变。这是异步的限制,我需要执行其他解决方案吗?

public class startTestAsync extends AsyncTask<Integer, Void, Void> {
    double[] timeArray = new double[500]; // set array
    double rating = 0;
    double lagStartTime;
    double lagEndTime;
    double lagTimeResult;
    int testAmount;
    int cnt;
    @Override
    protected Void doInBackground(Integer...grayVal) {
        for (testAmount = 0; testAmount < 500; testAmount++) {
            Log.i("Mat Value", String.valueOf(grayVal[0]));
            lagStartTime = System.nanoTime();
            while (grayVal[0] <= 100) {
                lagEndTime = System.nanoTime();
                lagTimeResult = (lagEndTime - lagStartTime);
                timeArray[testAmount] = lagTimeResult;
         }
         return null;
 }
 protected void onPostExecute(Long result) {
 }

ui线程
public void startTest (View view) {
    new startTestAsync().execute(grayVal);
}

@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
    grayMat = inputFrame.gray();
    double[] grayPixel = grayMat.get(100,100);
    grayVal = (int)grayPixel[0];
    Log.i("Mat Value", String.valueOf(grayVal));
    return grayMat;
}

onCameraFrame logcat中的grayVal
08-25 08:28:56.576  14645-14989/bluemage.systems.displaylatencytester I/Mat Value﹕ 50
08-25 08:28:56.639  14645-14989/bluemage.systems.displaylatencytester I/Mat Value﹕ 37
08-25 08:28:56.703  14645-14989/bluemage.systems.displaylatencytester I/Mat Value﹕ 52
08-25 08:28:56.769  14645-14989/bluemage.systems.displaylatencytester I/Mat Value﹕ 53
08-25 08:28:56.835  14645-14989/bluemage.systems.displaylatencytester I/Mat Value﹕ 50
08-25 08:28:56.903  14645-14989/bluemage.systems.displaylatencytester I/Mat Value﹕ 48
08-25 08:28:56.971  14645-14989/bluemage.systems.displaylatencytester I/Mat Value﹕ 47
08-25 08:28:57.038  14645-14989/bluemage.systems.displaylatencytester I/Mat Value﹕ 49
08-25 08:28:57.103  14645-14989/bluemage.systems.displaylatencytester I/Mat Value﹕ 46
08-25 08:28:57.172  14645-14989/bluemage.systems.displaylatencytester I/Mat Value﹕ 46
08-25 08:28:57.240  14645-14989/bluemage.systems.displaylatencytester I/Mat Value﹕ 43
08-25 08:28:57.302  14645-14989/bluemage.systems.displaylatencytester I/Mat Value﹕ 41
08-25 08:28:57.368  14645-14989/bluemage.systems.displaylatencytester I/Mat Value﹕ 44
08-25 08:28:57.436  14645-14989/bluemage.systems.displaylatencytester I/Mat Value﹕ 46

异步logcat中的grayVal
08-25 08:32:01.111  20759-21076/bluemage.systems.displaylatencytester I/Mat Value﹕ 133
08-25 08:32:01.111  20759-21076/bluemage.systems.displaylatencytester I/Mat Value﹕ 133
08-25 08:32:01.111  20759-21076/bluemage.systems.displaylatencytester I/Mat Value﹕ 133
08-25 08:32:01.111  20759-21076/bluemage.systems.displaylatencytester I/Mat Value﹕ 133
08-25 08:32:01.112  20759-21076/bluemage.systems.displaylatencytester I/Mat Value﹕ 133
08-25 08:32:01.112  20759-21076/bluemage.systems.displaylatencytester I/Mat Value﹕ 133
08-25 08:32:01.112  20759-21076/bluemage.systems.displaylatencytester I/Mat Value﹕ 133
08-25 08:32:01.112  20759-21076/bluemage.systems.displaylatencytester I/Mat Value﹕ 133
08-25 08:32:01.114  20759-21076/bluemage.systems.displaylatencytester I/Mat Value﹕ 133
08-25 08:32:01.114  20759-21076/bluemage.systems.displaylatencytester I/Mat Value﹕ 133
08-25 08:32:01.114  20759-21076/bluemage.systems.displaylatencytester I/Mat Value﹕ 133
08-25 08:32:01.114  20759-21076/bluemage.systems.displaylatencytester I/Mat Value﹕ 133
08-25 08:32:01.114  20759-21076/bluemage.systems.displaylatencytester I/Mat Value﹕ 133
08-25 08:32:01.114  20759-21076/bluemage.systems.displaylatencytester I/Mat Value﹕ 133
08-25 08:32:01.114  20759-21076/bluemage.systems.displaylatencytester I/Mat Value﹕ 133

均在logat中使用grayVal
08-25 09:02:49.170    6402-7189/bluemage.systems.displaylatencytester I/Mat Value Async﹕ 23
08-25 09:02:49.170    6402-7189/bluemage.systems.displaylatencytester I/Mat Value Async﹕ 23
08-25 09:02:49.170    6402-7189/bluemage.systems.displaylatencytester I/Mat Value Async﹕ 23
08-25 09:02:49.170    6402-6843/bluemage.systems.displaylatencytester I/Mat Value﹕ 22
08-25 09:02:49.170    6402-7189/bluemage.systems.displaylatencytester I/Mat Value Async﹕ 23
08-25 09:02:49.170    6402-7189/bluemage.systems.displaylatencytester I/Mat Value Async﹕ 23

最佳答案

grayVal(在doInBackground中)和grayVal(在onCameraFrame中)不同
因此,一个变量的变化不会影响另一变量。

即使没有完整的代码,也可以断定它们是不同的变量,因为它们具有不同的不兼容类型。
grayVal(在doInBackground中)是一个数组,可以从以下代码推导得出:

        Log.i("Mat Value", String.valueOf(grayVal[0]));
        lagStartTime = System.nanoTime();
        while (grayVal[0] <= 100) {
grayVal(在onCameraFrame中)是原始类型(或其包装类),可以从以下代码推导得出:
grayVal = (int)grayPixel[0];

关于java - 动态变量和异步,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32203734/

10-10 06:10