AugmentedImageDatabase Arcore:添加多个图像

时间:2019-03-21 08:33:48

标签: java android arcore

我有包含图像的文件夹,但无法将其添加到数据库。它们有特定的名称。应用程序显示“ AR会话失败”,这是我的代码:

  public void setupDatabase(Config config, Session session)
    {
        String filepath = getIntent().getStringExtra("filepath");
        Log.d ("AID", "Veriable:" + filepath);
        File dir = new File (filepath);
        File[] files = dir.listFiles();

        AugmentedImageDatabase aid = new AugmentedImageDatabase(session);

        for (int i = 0; i < files.length; i++)
        {
            if (files[i].getName().startsWith("arimage"))
            {
                String filename = files[i].getName ();
                Log.d ("AID", "FileName:" + files[i].getName ());
                Bitmap arcontentBitmap =loadAugmentedImage(files[i]);
                Log.d ("AID", "Files:" + files[i].getPath ());
                String imagename = filename.substring(0, filename.length() - 4);
                Log.d ("AID", "NameImage:" + imagename);

                aid.addImage(imagename, arcontentBitmap);

            }

        }
        config.setAugmentedImageDatabase(aid);
    }

方法loadAugmentedImage

private Bitmap loadAugmentedImage(File file)
    {
        try (InputStream is = new FileInputStream (file))
        {
            return BitmapFactory.decodeStream(is);
        }
        catch (IOException e)
        {
            Log.e("ImageLoad", "IO Exception", e);
        }
        return null;
    }

2 个答案:

答案 0 :(得分:0)

尝试将InputStream is = new FileInputStream (file)替换为InputStream is = getAssets().open(file)

我看到,您创建了新的图像数据库,然后加载了所有图像。这不是一个好习惯,因为加载一张图像大约需要30毫秒。因此,如果您从一开始就需要所有这些图像,那么应该在编译时加载图像数据库。您应该使用arcoreimg创建数据库,此选项还可以设置现实世界中图像的大小,这有助于检测。比仅使用以下方式加载数据库:

AugmentedImageDatabase augmentedImageDatabase = new AugmentedImageDatabase(session);
    try(InputStream inputStream = getAssets().open("imgdatabase.imgdb")){
        augmentedImageDatabase = AugmentedImageDatabase.deserialize(session, inputStream);
    } catch (IOException e) {
        Log.e(TAG, "IO exception loading augmented image database.", e);
        e.printStackTrace();
    }
    config.setAugmentedImageDatabase(augmentedImageDatabase);

但是请不要在应用程序开始时加载不需要的图像。因为您的应用将尝试检测所有图像,这可能会对性能产生重大影响。

答案 1 :(得分:-1)

我将下载文件和解压缩方法的缓冲区大小从1024更改为4096。我将完整的图像路径放置到我的方法loadAugmentedImage中。

public void setupDatabase(Config config, Session session) 
{
        AugmentedImageDatabase aid = new AugmentedImageDatabase(session);
        Intent intent = getIntent ();
        String filepath = intent.getStringExtra ("filepath");
        Log.d ("AID", "Veriable:" + filepath);
        File directory = new File (filepath);
        File[] files = directory.listFiles ();
        Log.d ("AID", "Size: " + files.length);
        for (int i = 0; i < files.length; i++)
        {

            if (files[i].getName().startsWith("arimage"))
            {
                String filename = files[i].getName ();
                Log.d ("AID", "FileName:" + filename);
                Bitmap arcontentBitmap = loadAugmentedImage(files[i].getPath ());
                Log.d ("AID", "FilePath:" + files[i].getPath ());
                Log.d ("AID", "Files:" + files[i].getAbsolutePath ());
                String imagename = filename.substring(0, filename.length() - 4);
                Log.d ("AID", "NameImage:" + imagename);
                aid.addImage(imagename, arcontentBitmap);
            }
        }
        config.setAugmentedImageDatabase(aid);
}

方法loadAugmentedImage

private Bitmap loadAugmentedImage(String file)
    {
        try (InputStream is = new FileInputStream (file))
        {
            return BitmapFactory.decodeStream(is);
        }
        catch (IOException e)
        {
            Log.e("loadAugmentedImage", "IO Exception", e);
        }
        return null;
    }