com.formkiq.vision.crafter.ImageToBlockExtractorFunction Maven / Gradle / Ivy
/*
* Copyright (C) 2018 FormKiQ Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.formkiq.vision.crafter;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import javax.xml.bind.DatatypeConverter;
import com.formkiq.vision.document.DocumentImage;
import com.formkiq.vision.document.DocumentSource;
/**
* {@link Function} to convert Images to {@link ImageBlockExtractor}.
*
*/
public class ImageToBlockExtractorFunction
implements Function> {
/** {@link DocumentSource}. */
private DocumentSource documentsource;
/**
* constructor.
* @param source {@link DocumentSource}
*/
public ImageToBlockExtractorFunction(final DocumentSource source) {
this.documentsource = source;
}
@Override
public List apply(final Integer pageNumber) {
List list = new ArrayList<>();
List images = this.documentsource.getImages(pageNumber);
for (DocumentImage image : images) {
ImageBlockExtractor e = new ImageBlockExtractor();
e.setImage(getPngString(image));
e.setX(image.getX());
e.setY(image.getY());
e.setWidth(image.getWidth());
e.setHeight(image.getHeight());
list.add(e);
}
return list;
}
/**
* Convert {@link DocumentImage} to PNG String.
* @param image {@link DocumentImage}
* @return {@link String}
*/
private String getPngString(final DocumentImage image) {
String data = DatatypeConverter.printBase64Binary(image.getImage()),
dataUrl = "data:image/" + image.getImagetype() + ";base64," + data;
return dataUrl;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy