如何更改void方法以在Java中返回值

时间:2015-12-28 16:28:35

标签: java android

所以我是java新手,并且正在使用某人的开源代码进行android开发。我想在另一个活动中使用此程序中的变量颜色。我尝试将void更改为int,然后在代码的末尾返回颜色,但这没有任何区别,代码不起作用。我很困惑为什么因为void没有返回任何东西,但是int返回一个整数。我的问题是如何从这段代码中获取变量颜色的值,并将其存储在另一个变量中,或者将变量本身变为颜色,以便我可以在我的Android应用程序的其他活动中使用它。这是我的代码:

public void onPreviewFrame(byte[] data, Camera camera) {

        try {
            Camera.Size previewSize = camera.getParameters().getPreviewSize();
            int height = previewSize.height;
            int width = previewSize.width;

            ColorModelConverter converter = new ColorModelConverter(height, width);
            int[] pixels = converter.convert(data, this.colorFormat);

            int color = pickColor(pixels, height, width);
            updateColorData(color);

            Log.i("FRAME PREVIEW", "Color updated");
        } catch (RuntimeException oops) {
            // Do nothing, exception is thrown because onPreviewFrame is called after camera is released
            Log.i("FRAME PREVIEW", "RuntimeException thrown into onPreviewFrame");
        }
    }

此处还有完整项目的github链接,如果这有所不同:https://github.com/adlebzelaznog/colometer

2 个答案:

答案 0 :(得分:3)

我认为问题是onPreviewFrame是一个由系统/框架而不是你调用的继承方法(假设它是这里提到的那个:http://developer.android.com/reference/android/hardware/Camera.PreviewCallback.html)。

在这种情况下,我会保持签名相同,然后将值保存在例如共享首选项,以便您可以访问应用程序其他部分的颜色值(http://developer.android.com/reference/android/content/SharedPreferences.html)。

您的代码看起来像这样:

public void onPreviewFrame(byte[] data, Camera camera) {

        try {
            Camera.Size previewSize = camera.getParameters().getPreviewSize();
            int height = previewSize.height;
            int width = previewSize.width;

            ColorModelConverter converter = new ColorModelConverter(height, width);
            int[] pixels = converter.convert(data, this.colorFormat);

            int color = pickColor(pixels, height, width);
            updateColorData(color);

            storeColorInSharedPreferences(color); //save variable here

            Log.i("FRAME PREVIEW", "Color updated");
        } catch (RuntimeException oops) {
            // Do nothing, exception is thrown because onPreviewFrame is called after camera is released
            Log.i("FRAME PREVIEW", "RuntimeException thrown into onPreviewFrame");
        }
    }

public void storeColorInSharedPreferences(int color)
{
    //Logic to store color in Shared Preferences
    //Something like this: (not tested!)
    SharedPreferences shared = getSharedPreferences("com.myapp.sharedpreferences", MODE_PRIVATE);
    SharedPreferences.Editor editor = shared.edit();
    editor.putInt("PREVIEW_KEY", color);
    editor.commit();
}

然后在另一个活动中你可以得到这样的值:

SharedPreferences shared = getSharedPreferences("com.myapp.sharedpreferences", MODE_PRIVATE);
int color = (shared.getInt("PREVIEW_KEY", 0));

示例摘自:Get Android shared preferences value in activity/normal class

答案 1 :(得分:0)

将方法返回类型更改为int,以便签名显示为public int onPreviewFrame(byte[] data, Camera camera)

然后,在try块的末尾,在Log.i("FRAME PREVIEW", "Color updated");之后,写下return color;

最后,在整个方法的最后,写return -1;来表示错误。