使用不同的库在Android和JVM上执行SAME程序

时间:2014-06-21 01:39:27

标签: java android cross-platform

我的代码需要在Linux机器和Android手机之间进行迁移。它进行基本的图像处理,并在Linux上使用ImageIO,在Android上使用BitmapFactory来加载jpeg图像。因此,我检测环境并调用适当的方法如下:

   if (isDalvik()) {
      Bitmap bi = BitmapFactory.decodeStream(this.getClass().getResourceAsStream("test.jpg"));                                                                                        
      this.findFaces_Android(bi, 1, 40, new File("result.jpg"));
   } else {
     BufferedImage bi = ImageIO.read(Exp_Starter.class.getResourceAsStream("test.jpg"));
     this.findFaces_J2SE(bi, 1, 40, new File("result.jpg"));
   }

这在Linux上工作正常,因为我已将android.jar放在类路径中。但是,它无法在Android上运行。启动时,它声称没有找到BufferedImage和ImageIO类,尽管它们实际上从未被调用,因为else语句不会被执行。

我尝试为Android应用程序创建java.awt.image.BufferedImage和javax.imageio.ImageIO的空类,但编译器不允许覆盖它们。

有关如何解决此问题并使SAME程序在不同环境中工作的任何想法吗?

1 个答案:

答案 0 :(得分:0)

显然,将不同运行时环境的代码移动到不同的类可以解决问题。这对我有用:

if (isDalvik()) {
  Find_Faces_Android ff = new Find_Faces_Android();   
  ff.readImage(this.getClass().getResourceAsStream("test.jpg"));
  int no_detected_faces = ff.findFaces( 1, 40, new File("result.jpg"));
} else {
  Find_Faces_JVM ff = new Find_Faces_JVM();
  ff.readImage(this.getClass().getResourceAsStream("test.jpg"));
  int no_detected_faces = ff.findFaces( 1, 40, new File("result.jpg"));
}