Search in sources :

Example 1 with Image

use of com.android.tools.pixelprobe.Image in project android by JetBrains.

the class LayeredImageConverter method toVectorDrawableXml.

/**
   * Loads the specified file as a pixelprobe {@link Image}, finds its vector layers
   * and converts them to a Vector Drawable XML representation.
   *
   * @param path The file to convert to Vector Drawable
   * @return The XML representation of a Vector Drawable
   *
   * @throws IOException If an error occur while parsing the file
   */
@NotNull
String toVectorDrawableXml(@NotNull File path) throws IOException {
    FileInputStream in = new FileInputStream(path);
    Image image = PixelProbe.probe(in, new Decoder.Options().decodeLayerImageData(false).decodeLayerTextData(false).decodeLayerAdjustmentData(false).decodeGuides(false));
    Rectangle2D.Double bounds = new Rectangle2D.Double(0.0, 0.0, image.getWidth(), image.getHeight());
    myFormat = createDecimalFormat((float) bounds.getWidth(), (float) bounds.getHeight());
    Element vector = new Element(SdkConstants.TAG_VECTOR);
    extractPathLayers(vector, image.getLayers());
    vector.attribute("width", String.valueOf((int) bounds.getWidth()) + "dp").attribute("height", String.valueOf((int) bounds.getHeight()) + "dp").attribute("viewportWidth", String.valueOf((int) bounds.getWidth())).attribute("viewportHeight", String.valueOf((int) bounds.getHeight()));
    String xml = toVectorDrawable(vector);
    in.close();
    return xml;
}
Also used : Image(com.android.tools.pixelprobe.Image) Decoder(com.android.tools.pixelprobe.decoder.Decoder) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with Image

use of com.android.tools.pixelprobe.Image in project android by JetBrains.

the class FileChooserActionListener method openDeviceChoiceDialog.

/**
   * Open a dialog asking to choose a device whose dimensions match those of the image
   */
private static void openDeviceChoiceDialog(VirtualFile virtualFile, @NotNull NlProperty fileProperty, @Nullable NlProperty cropProperty) {
    if (virtualFile.exists() && !virtualFile.isDirectory()) {
        try {
            final Image probe = PixelProbe.probe(virtualFile.getInputStream());
            final BufferedImage image = probe.getMergedImage();
            if (image == null) {
                return;
            }
            final NlModel model = fileProperty.getModel();
            final Configuration configuration = model.getConfiguration();
            final Device device = configuration.getDevice();
            if (device == null) {
                return;
            }
            ApplicationManager.getApplication().invokeLater(() -> {
                final DeviceSelectionPopup deviceSelectionPopup = new DeviceSelectionPopup(model.getProject(), configuration, image);
                if (deviceSelectionPopup.showAndGet()) {
                    saveMockupFile(virtualFile, fileProperty, cropProperty);
                }
            });
        } catch (IOException e1) {
            LOGGER.warn("Unable to open this file\n" + e1.getMessage());
        }
    }
}
Also used : Configuration(com.android.tools.idea.configurations.Configuration) Device(com.android.sdklib.devices.Device) NlModel(com.android.tools.idea.uibuilder.model.NlModel) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage) Image(com.android.tools.pixelprobe.Image) BufferedImage(java.awt.image.BufferedImage)

Example 3 with Image

use of com.android.tools.pixelprobe.Image in project android by JetBrains.

the class MockupFileHelper method openImageFile.

@Nullable
public static Image openImageFile(String path) {
    Image image = null;
    final File file = new File(path);
    if (!file.exists()) {
        return null;
    }
    if (IMAGE_CACHE.containsKey(path)) {
        return IMAGE_CACHE.get(path);
    }
    try (FileInputStream in = new FileInputStream(file)) {
        image = PixelProbe.probe(in, Decoder.Options.LAYER_METADATA_ONLY);
        IMAGE_CACHE.put(path, image);
    } catch (IOException e) {
        Logger.getInstance(MockupLayer.class).error(e);
    }
    return image;
}
Also used : IOException(java.io.IOException) Image(com.android.tools.pixelprobe.Image) File(java.io.File) FileInputStream(java.io.FileInputStream) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

Image (com.android.tools.pixelprobe.Image)3 IOException (java.io.IOException)2 Device (com.android.sdklib.devices.Device)1 Configuration (com.android.tools.idea.configurations.Configuration)1 NlModel (com.android.tools.idea.uibuilder.model.NlModel)1 Decoder (com.android.tools.pixelprobe.decoder.Decoder)1 BufferedImage (java.awt.image.BufferedImage)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 NotNull (org.jetbrains.annotations.NotNull)1 Nullable (org.jetbrains.annotations.Nullable)1