We're Hiring!

Import image file via Java API

General and open developer discussion about using OMERO APIs from C++, Java, Python, Matlab and more! Please new questions at https://forum.image.sc/tags/omero
Please note:
Historical discussions about OMERO. Please look for and ask new questions at https://forum.image.sc/tags/omero

If you are having trouble with custom code, please provide a link to a public repository, ideally GitHub.

Import image file via Java API

Postby dwj » Mon Jan 04, 2016 2:40 pm

Hi,

I try to import imag files to the omero server via the Java API.
Unfortunately I can't get an instance of an importer to start the uploading procress.

Code: Select all
File file = new File(“Path to my image file”);
FileObject fileObject = new FileObject(file);
ImportableFile importFile = new ImportableFile(fileObject, false);
List<ImportableFile> fileList = new ArrayList<>();
fileList.add(importFile);

ImportableObject importObject = new ImportableObject(fileList, false);

boolean exists = ImporterFactory.doesImporterExist(); <-- always FALSE

//crashes with NullPointerException
Importer importer = ImporterFactory.getImporter(user.getGroupId(), true, 0);


How can I got an importer?

Thanks for your help.
dwj
 
Posts: 23
Joined: Mon Nov 16, 2015 10:46 am

Re: Import image file via Java API

Postby Dominik » Tue Jan 05, 2016 11:37 am

It looks like you're trying to use the Insight code. That's quite complex, because it deals with a lot of use cases (importing for different users, creating projects/datasets automatically, etc.).
Here's a simple example for importing images into an already existing dataset (with id 1 in that case) using the OMEROMetadataStoreClient from the Java API directly:

Code: Select all
public void uploadImages(String... paths) {
       
        ImportConfig config = new ome.formats.importer.ImportConfig();

        config.email.set("");
        config.sendFiles.set(true);
        config.sendReport.set(false);
        config.contOnError.set(false);
        config.debug.set(false);

        config.hostname.set("localhost");
        config.port.set(4064);
        config.username.set("root");
        config.password.set("omero");
       
        config.targetClass.set("omero.model.Dataset");
        config.targetId.set(1L);
       
        OMEROMetadataStoreClient store;
        try {
            store = config.createStore();
            store.logVersionInfo(config.getIniVersionNumber());
            OMEROWrapper reader = new OMEROWrapper(config);
            ImportLibrary library = new ImportLibrary(store, reader);

            ErrorHandler handler = new ErrorHandler(config);
            library.addObserver(new LoggingImportMonitor());

            ImportCandidates candidates = new ImportCandidates(reader, paths, handler);
            reader.setMetadataOptions(new DefaultMetadataOptions(MetadataLevel.ALL));
            library.importCandidates(config, candidates);

            store.logout();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


I'll also add this to the Java examples in our developer documentation, unfortunately a example for this task is still missing in the documentation for now.

Regards,
Dominik
User avatar
Dominik
Team Member
 
Posts: 149
Joined: Mon Feb 10, 2014 11:26 am

Re: Import image file via Java API

Postby dwj » Tue Jan 05, 2016 2:42 pm

Hi Dominik!

Thank you very much! It works perfectly!

Best regards,
Daniel
dwj
 
Posts: 23
Joined: Mon Nov 16, 2015 10:46 am

Re: Import image file via Java API

Postby Dominik » Wed Jan 06, 2016 9:15 am

Hi Daniel,

you're welcome. I forgot to mention, you could also use the Gateway (which makes dealing with the Java API a bit easier), but the Gateway is not stable yet (classes might move into different packages, method signatures might change, etc.).
See http://downloads.openmicroscopy.org/ome ... teway.html (in particular for importing images: http://downloads.openmicroscopy.org/ome ... ility.html ) How to use the Gateway to connect to the server see https://www.openmicroscopy.org/site/sup ... /Java.html

Regards,
Dominik
User avatar
Dominik
Team Member
 
Posts: 149
Joined: Mon Feb 10, 2014 11:26 am


Return to Developer Discussion

Who is online

Users browsing this forum: No registered users and 1 guest