Use processing code as a separate class in Android app project on Eclipse ?

edited November 2015 in Android Mode

Hi ! I've been working on making a small app that allows me to run a processing code from my main activity. The code (we called it kMeans class) is in my src folder along with mainActivity (which is android code only and runs kMeans independantly). The only thing to know about the latter is that I create a bitmap at some point in my mainActivity which I transfer to kMeans using the initialize(Bitmap img) function.

So the problem I am encountering might be really simple to solve because I am probably not implementing my processing class properly in my App project, but I have looked at many forums regarding my problem and most people use processing in their mainActivity (calling draw and setup there). Whereas I am trying to keep my processing code separated from my mainActivity which only uses android code.
quickNote: I have added android-core to the libs and it is recognized. My problem is: most of the PApplet functions (red(), brightness(), colorMode()) do not work and return NullPointerException. Does anyone know why? What am I forgetting?

Thank in advance for your help !!!!

ps: sorry if this isn't the right place where to ask this question. I have been trying to solve this for two days and couldn't find a solution...

Here is the part of code from mainActivity where I try running my processing code (kMeans):

                if (bitmap != null){
                    System.out.println("processing Image");
                    processImage = new kMeans();
                    processImage.initialize(bitmap);

                    int[] pix = processImage.getResult();
                    //make Bitmap mutable so that we can use setPixels
                    Bitmap test = bitmap.copy(Bitmap.Config.ARGB_8888, true);

                    for (int x=0;x<test.getWidth(); x++){
                        for (int y=0; y< test.getHeight(); y++){
                            test.setPixel(x, y, pix[x+y*(test.getWidth())]);                                

                        }//for y    
                    }//for x
                    bitmap = test;
                    imageView.setImageBitmap(bitmap);
                    System.out.println("worked?");
                }

Works perfectly when I just run through the bitmap in kMeans and return a greyScale Image. Here is part of kMeans (it's super long so if you think you need more of the code let know !):

import android.app.Activity; import android.graphics.Bitmap; import android.graphics.Color; import android.os.Bundle; import processing.core.*;

public class kMeans extends PApplet { //Original, unaltegetRed Image PImage oImage; //Final image PImage fImage; //kmeans image PImage kImage; //Image with kmeans lines PImage lImage; //Image with xdog lines PImage xImage;

//Number if groups for kmeans
int numGroups = 8;
//How much do we blur our image?
int blurFactor = 1;
//the larger blur for xdog should be 1.6 times bigger than the smaller one
float kernelDif = (float)(1.6);

int sW;
int sH;


@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
}

public kMeans(){

};

public void initialize(Bitmap img){
    oImage = createImage(img.getWidth(),img.getHeight(),RGB);
    oImage.loadPixels();
    img = img.copy(Bitmap.Config.ARGB_8888, true);
    for (int x=0;x<img.getWidth(); x++){
        for (int y=0; y< img.getHeight(); y++){
            int location = x + y*img.getWidth();
            img.setPixel(x,y,Color.GRAY);
            oImage.pixels[location]= img.getPixel(x, y);

        }//for y    
    }//for x
    //float p.getRed = p.getRed(25);
    //System.out.print(p.p.getRed);
    oImage.updatePixels();
    System.out.print("image kmean oiamge done");
    setup();
}

public void setup(){
      //oImage = loadImage(location);
      //oImage.resize((int)(1.5*640),(int)(1.5*480));

      sW = oImage.width;
      sH = oImage.height;

      //System.out.print("Starting kmeans");
      kImage = kMeans(oImage,numGroups);
      //System.out.print("Done with kmeans");

      size(4*oImage.width,oImage.height);
      frameRate(10);

      //System.out.print("Starting line detection");
      lImage = kMeanLine(kImage);
       xImage = threshDoG(oImage);
      //System.out.print("Done with line detecing");

      //System.out.print("Starting sharpening");
      lImage = unSharpMaskLocal(oImage,lImage,blurFactor);
      xImage = unSharpMaskLocal(oImage,xImage,blurFactor);
   }

    PImage unSharpMask(PImage img,float blurFactor){  
      //Return this image
      PImage ret = createImage(sW,sH,ARGB);

      //Make a blurgetRed copy of the image
      PImage blurImage = createImage(sW,sH,ARGB);
      blurImage.copy(img,0,0,sW,sH,0,0,sW,sH);
      blurImage.filter(BLUR,blurFactor);

      //img-blur = diff, ret = diff + img
      colorMode(RGB);
      for(int i = 0; i< ret.pixels.length;i++){
        //newPixelValue = (imgPixelValue - getBluePixelValue) + imgPixelValue
        float difR = (2*getRed(img.pixels[i])) -getRed(blurImage.pixels[i]);
        float difG = (2*getGreen(img.pixels[i])) -getGreen(blurImage.pixels[i]);
        float difB = (2*getBlue(img.pixels[i])) -getBlue(blurImage.pixels[i]);
        ret.pixels[i] = color(difR,difG,difB);
      }
      return ret;
    }

And here is the error: 12-16 00:11:43.924: E/AndroidRuntime(7259): FATAL EXCEPTION: main 12-16 00:11:43.924: E/AndroidRuntime(7259): Process: bard.edu.focus, PID: 7259 12-16 00:11:43.924: E/AndroidRuntime(7259): java.lang.NullPointerException 12-16 00:11:43.924: E/AndroidRuntime(7259): at processing.core.PApplet.colorMode(Unknown Source) 12-16 00:11:43.924: E/AndroidRuntime(7259): at bard.edu.focus.kMeans.unSharpMaskLocal(kMeans.java:162) 12-16 00:11:43.924: E/AndroidRuntime(7259): at bard.edu.focus.kMeans.setup(kMeans.java:104) 12-16 00:11:43.924: E/AndroidRuntime(7259): at bard.edu.focus.kMeans.initialize(kMeans.java:74) 12-16 00:11:43.924: E/AndroidRuntime(7259): at bard.edu.focus.MainActivity$PlaceholderFragment$2.onClick(MainActivity.java:124) 12-16 00:11:43.924: E/AndroidRuntime(7259): at android.view.View.performClick(View.java:4753) 12-16 00:11:43.924: E/AndroidRuntime(7259): at android.view.View$PerformClick.run(View.java:19562) 12-16 00:11:43.924: E/AndroidRuntime(7259): at android.os.Handler.handleCallback(Handler.java:733) 12-16 00:11:43.924: E/AndroidRuntime(7259): at android.os.Handler.dispatchMessage(Handler.java:95) 12-16 00:11:43.924: E/AndroidRuntime(7259): at android.os.Looper.loop(Looper.java:146) 12-16 00:11:43.924: E/AndroidRuntime(7259): at android.app.ActivityThread.main(ActivityThread.java:5635) 12-16 00:11:43.924: E/AndroidRuntime(7259): at java.lang.reflect.Method.invokeNative(Native Method) 12-16 00:11:43.924: E/AndroidRuntime(7259): at java.lang.reflect.Method.invoke(Method.java:515) 12-16 00:11:43.924: E/AndroidRuntime(7259): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291) 12-16 00:11:43.924: E/AndroidRuntime(7259): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107) 12-16 00:11:43.924: E/AndroidRuntime(7259): at dalvik.system.NativeStart.main(Native Method)

Tagged:

Answers

  • I've got something kinda similar working and I think I can help, but first a couple questions. where is initialize getting called from? it's not a method on either PApplet or Activity, and also why are you calling setup from there? Are you overriding onCreate for a reason? it doesn't look like it. also side note, there's always an implicit empty constructor, no need to create one... and that might also be screwing with things.

    I'm asking because with something interfering with the creation of a PApplet when you don't have to is usually a bad idea. Simply extending PApplet and overriding the setup and draw methods should be enough.

    also try switching from: import processing.core.*

    to: kMeans extends processing.core.PApplet

    for no good reason other than that's how I have it and it works... it really shouldn't make a difference but I swear sometimes this stuff works by magic.

  • oh! and one last thing, if you're using that initialize method to pass data from your android code to your processing, use an intent instead. android is really picky about how you share data between activities

  • shoot, I just saw the other part of your code >.< the problem could also be with the way you're launching your activity. in android, the best way to do this is with an intent. so you want to set it up like:

    Intent intent = new Intent(mainActivity.this, kMeans.class); startActivity(intent);

Sign In or Register to comment.