1

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

  • 1
    If you change from `void` to `int` then add a `return` statement as well in the method, which returns the value. – YoungHobbit Dec 28 '15 at 16:30
  • `onPreviewFrame` is the callback from `android.hardware.Camera.PreviewCallback`. Not sure how you managed to change the `void` to `int` and your code still works – Jay Dec 28 '15 at 16:36

2 Answers2

3

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 e.g. 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

Community
  • 1
  • 1
Dyrborg
  • 877
  • 7
  • 16
  • What do you mean by the logic to store color? – Nikhil Gopal Dec 28 '15 at 16:59
  • I've added a code example - see a deeper explanation on how to get the value where you need it in your app here: http://stackoverflow.com/questions/2614719/how-do-i-get-the-sharedpreferences-from-a-preferenceactivity-in-android – Dyrborg Dec 28 '15 at 18:14
  • @NikhilGopal - did this answer help you? – Dyrborg May 27 '16 at 13:15
0

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.