[英]How to change a void method to return a value in Java
So I am new to java and am using someone's open source code for android development. 所以我是java的新手,并且正在使用某人的开源代码进行android开发。 I want to use the variable color in this program in another activity.
我想在另一个活动中使用此程序中的变量颜色。 I tried changing void to int and then returning color at the end of the code but that does not make a difference and the code does not work.
我尝试将void更改为int,然后在代码的末尾返回颜色,但这没有任何区别,代码不起作用。 I am confused why as void doesn't return anything but an int returns an integer.
我很困惑为什么因为void不返回任何东西,但是int返回一个整数。 My question is how can I get the value of the variable color from this code and either store it in another variable or make the variable color itself so that I can use it in other activities of my android app.
我的问题是如何从这段代码中获取变量颜色的值,并将其存储在另一个变量中,或者将变量本身变为颜色,以便我可以在我的Android应用程序的其他活动中使用它。 Here is my code:
这是我的代码:
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");
}
}
Also here is the github link to the full project if that makes a difference: https://github.com/adlebzelaznog/colometer 如果有所不同,这里还有完整项目的github链接: https : //github.com/adlebzelaznog/colometer
I assume the issue is that onPreviewFrame is an inheritered method that is called by the system / framework and not by you (assuming it is the one mentioned here: http://developer.android.com/reference/android/hardware/Camera.PreviewCallback.html ). 我假设问题是onPreviewFrame是一个由系统/框架而不是你调用的继承方法(假设它是这里提到的那个: http : //developer.android.com/reference/android/hardware/Camera。 PreviewCallback.html )。
In that case I would keep the signature the same, and then save the value in eg shared preferences, so that you can access the color value in other parts of your application ( http://developer.android.com/reference/android/content/SharedPreferences.html ). 在这种情况下,我会保持签名相同,然后将值保存在例如共享首选项中,以便您可以访问应用程序其他部分的颜色值( http://developer.android.com/reference/android/ content / SharedPreferences.html )。
Your code would look something like this: 您的代码看起来像这样:
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();
}
Then in another activity you can get the value like this: 然后在另一个活动中,您可以获得如下值:
SharedPreferences shared = getSharedPreferences("com.myapp.sharedpreferences", MODE_PRIVATE);
int color = (shared.getInt("PREVIEW_KEY", 0));
Example taken from: Get Android shared preferences value in activity/normal class 示例取自: 获取活动/普通类中的Android共享首选项值
Change the method return type to int
, so that the signature reads public int onPreviewFrame(byte[] data, Camera camera)
. 将方法返回类型更改为
int
,以便签名读取public int onPreviewFrame(byte[] data, Camera camera)
。
Then, at the end of your try
block, after Log.i("FRAME PREVIEW", "Color updated");
然后,在
try
块结束时,在Log.i("FRAME PREVIEW", "Color updated");
之后Log.i("FRAME PREVIEW", "Color updated");
, write return color;
,写
return color;
. 。
Finally, at the end of the entire method, write return -1;
最后,在整个方法结束时,写入
return -1;
to represent an error. 表示错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.