Building an API in Processing?

I’ve researched API’s briefly, and my overall goal would be much simpler if I could build an API in Processing from a program that I’ve already built and then access that through an iOS app that I plan on building after. Any thoughts?

Not sure what you mean by “build an API in Processing.” In the sense that you want your iOS to pass some parameters to a processing and for it to generate an image file and return that image file to the iOS app? Do you want p5.js code inside a self-contained iOS app, or do you want an iOS app that is interacting with a p5.js or processing-js webserver? Will the Processing “API” need access to iOS touch / sensor data, or will it just be some parameters. There are many ways to work with Processing on many platforms, although iOS is one of the hardest ways. For good advice, I’d suggest giving a lot more detail.

1 Like

No problem, usually I give too many unnecessary details.
I made a program in Processing (Java) that takes a photo and analyzes the pixel data going through determining which pixels are green vs. white in a given area, and then also measuring by turning the picture black and white and checking for a change in pixel value from black to white.
The idea is to now make this into an iPhone app, and I assumed I was just going to base my code off of the original program and try and rewrite it in a different language (swift). But I heard about API’s where I can send data (in my case an image from an iPhone) to a server that then analyzes that data and sends back the values/ measurements. This would allow for me to use the original code I made in Java (if I can create an API in Java) and just do a little bit of coding for the app itself.
I’m about a month and a half new to coding, so any assumptions I made, you are right to question them.

provided you have a java hosting server, you can try making a server via java.net.* standard libraries in java, and use processing core as a library to run your java sketch, then when the server receives a connection through a java socket you can maybe pass in the received image and process it, returning the output back through the socket connection.

However, this is a lot more difficult to set up and probably slower due to network latency than just doing in the app itself, with the added bonus in that it doenst need a internet connection to run either. If you wish to pursue this approach then go for it, but keep in mind you’ll now need to maintain two sets of systems which need to reliably communicate with each other.

1 Like

Okay – What are you planning on using as the server? Does it have a static IP? Is it a desktop machine?

I ask because Processing is normally tied to desktop (as opposed to headless) running. If this isn’t an always-on server, then you can just run a sketch out of PDE using the server-client networking library:

Send a message to the desktop “Server” sketch, and have it send a message back.

However, if you are doing mobile image analysis, an easier path might be to use one of the Javascript forms of Processing – p5.js or processing.js – and do a client-only iOS app, either with the javascript embedded in the app or with it loaded over the web, then executed in a mobile browser – then you don’t have to upload an image to analyze it. There are examples of incorporating processing.js into iOS apps in XCode on github – however this is not easy. You may find learning p5.js (which is very similar to Java Processing) easier – and then you could deploy it as a web service.

Of course, the other option is to just learn to write image analysis code in Swift directly sans processing.

3 Likes

Thank you for your input. I’ve been pushing the idea of writing the code directly in Swift off out of fear of the language. Processing was my first language I learned and it was so intuitive and Dan Shiffman was by far the greatest teacher during the process. Now looking at Swift there is so much going on and so many crazy names of classes and stuff.
Any tips on learning a second language? I feel like it shouldn’t be hard, but that’s not what I’ve found to be the case.
Secondly, if you have absolutely any resources you recommend regarding learning Swift/xCode or image processing in iOS, let me know. I’m in a major slump right now and not sure where to invest my limited time I have to learn this stuff. Thanks

In case anyone else wants to protoype something like that with Processing in an Android device take a look at http://phonk.io/ :slight_smile: The old version of the website seems to have more details about how it works: http://protocoder.org/

2 Likes

I don’t really have iOS dev expertise, unfortunately. If you are feeling stuck and confortable with Processing and Dan Shiffman, then you might want to seriously consider the p5.js route – with Dan Shiffman’s tutorials – as it is a different but related syntax and an almost identical API.

I second this.

Even with your relative inexperience, I actually think your goal is doable.

But with any problem, you’re gonna want to break it down into more bite-size problems. Here’s my breakdown:

The idea is to now make this into an iPhone app,

  • This is probably the biggest step, and might require some further breaking-down

…send data (in my case an image from an iPhone)…

  • look into sending GET requests with you language of choice

…to a server…

…that then analyzes that data…

  • Sounds like you’ve already built this part?
  • However, if you’re using nodejs, it might have to be rewritten in Javascript

…and sends back the values/ measurements.

  • Similar to sending a request, you’re going to have learn how Javascript or Swift handles requests. Some googling turned up this

Of course, anything’s doable with enough time, so another question that needs to be answered is how much time you have to commit to this

1 Like

Much appreciated. You made a good point about time as well as breaking things down. I think I’ll be able to allocate a good amount of time to building the app. With that said I think it’d be better if the image processing was done locally without the need for a relatively fast connection (it will likely be used in remote places). My next step is to just sit through a ton of Swift/xCode tutorials. Thank you very much.