Search This Blog

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

No comments:

Post a Comment