Search This Blog

21 September 2017

Digital life

    Let’s code a little JavaScript project.
We have a world/ labyrinth / map and some creatures that roam in it. The world is described with walls and movable space. The creatures can use only the movable space to move and cannot move through the walls. The creatures have a field of vision of 1 square around them.

We will use some symbols to denote each one of the objects in our world:
    wall - *
    movable space - white space (space " ")
    creature - o

We will store the world in a two dimensional array. This way we can denote a location in the world with:
    world[x][y]

We could have used a one dimensional array. Then a location in the world would be:
    index = y * width + x
    world[index]



Here is the small repository of the project:

09 September 2017

Marvin framework on Android

    If you remember our Sentinel application, we considered using Marvin Framework for the image comparison, but it uses some Java packages that are not available on Android.
    Now we take a second look at it.
    Well it turns out that we don't want to port it, because it is using awt and that is a lot of work that we don't want to do right now.
    But here is an example of using Marvin Framework on Android. We are using the GrayScale filter. We have updated the instance variable "image" of the MarvingImage class to use a Bitmap instead of a BufferedImage:
// Image 
protected Bitmap image; 
And here is the code to apply a GrayScale filter:
1:          File fileOut = new File(getCacheDir(), "image.png");  
2:          BufferedInputStream inputStream = null;  
3:          FileOutputStream fos = null;  
4:          try {  
5:            inputStream = new BufferedInputStream(getAssets().open("ic_launcher.png"));  
6:            fos = new FileOutputStream(fileOut);  
7:            byte buffer[] = new byte[8192];  
8:            int length = 0;  
9:            while((length = inputStream.read(buffer)) > 0) {  
10:              fos.write(buffer, 0, length);  
11:            }  
12:            fos.flush();  
13:          } catch (IOException e) {  
14:            e.printStackTrace();  
15:          } finally {  
16:            if(inputStream != null) {  
17:              try {inputStream.close();} catch (IOException e) {/* do nothing */}  
18:            }  
19:            if(fos != null) {  
20:              try {fos.close();} catch (IOException e) {/* do nothing */}  
21:            }  
22:          }  
23:    
24:          String filePath = fileOut.getPath();  
25:          Log.i(TAG, "filePath=" + filePath + ", absolutePath=" + fileOut.getAbsolutePath());  
26:    
27:          //Change the ivar  
28:          // BufferedImage image  
29:          //int MarvinImage with Bitmap  
30:          Bitmap myBitmap = BitmapFactory.decodeFile(filePath);  
31:          int[] pixels = new int[myBitmap.getWidth() * myBitmap.getHeight()];  
32:          myBitmap.getPixels(pixels, 0, myBitmap.getWidth(), 0, 0, myBitmap.getWidth(), myBitmap.getHeight());  
33:          MarvinImage image = new MarvinImage(myBitmap.getWidth(), myBitmap.getHeight());  
34:          image.setIntColorArray(pixels);  
35:    
36:          GrayScale grayScale = new GrayScale();  
37:          grayScale.load();  
38:          MarvinImagePlugin imagePlugin = grayScale;  
39:          //MarvinImagePlugin imagePlugin = MarvinPluginLoader.loadImagePlugin("org.marvinproject.image.color.grayScale.jar");  
40:          imagePlugin.process(image, image);  
41:          image.update();  
42:    
43:          //implement the  
44:          // MarvinImageIO->loadImage  
45:          // MarvinImageIO->saveImage  
46:          //so that they don't rely on the javax package  
47:  //        MarvinImage image = MarvinImageIO.loadImage(filePath);  
48:  //        MarvinImagePlugin imagePlugin = MarvinPluginLoader.loadImagePlugin("org.marvinproject.image.color.grayScale.jar");  
49:  //        imagePlugin.process(image, image);  
50:  //        image.update();  
51:    
52:          int[] colorArray = image.getIntColorArray();  
53:          Bitmap bitmap = Bitmap.createBitmap(colorArray, myBitmap.getWidth(), myBitmap.getHeight(), Bitmap.Config.ARGB_8888);  
54:          mImageViewMain.setBackgroundDrawable(new BitmapDrawable(bitmap));  

Building the application


    When I am working with a technology, I am like - how other people think and say it is difficult, it is easy stuff.
    And when I look in to a new language or technology stack, I am like - oh I have all these tools and things, now I understand why someone would say it is difficult, because it is.
    And after a few days or weeks when I am over the hump, I am again to my old self - it is not difficult at all, I can't believe I considered it to be difficult, and how can anyone say it is.
Keep hustling.