简体   繁体   中英

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. 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. I am confused why as void doesn't return anything but an int returns an integer. 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. 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

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 ).

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 ).

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

Change the method return type to int , so that the signature reads public int onPreviewFrame(byte[] data, Camera camera) .

Then, at the end of your try block, after Log.i("FRAME PREVIEW", "Color updated"); , write return color; .

Finally, at the end of the entire method, write return -1; to represent an error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM