Search in sources :

Example 11 with EncodedImage

use of com.codename1.ui.EncodedImage in project CodenameOne by codenameone.

the class EncodedImage method scaledEncoded.

/**
 * Performs scaling using ImageIO to generate an encoded Image
 * @param width the width of the image, -1 to scale based on height and preserve aspect ratio
 * @param height the height of the image, -1 to scale based on width and preserve aspect ratio
 * @return new encoded image
 */
public EncodedImage scaledEncoded(int width, int height) {
    if (width == getWidth() && height == getHeight()) {
        return this;
    }
    if (width < 0) {
        float ratio = ((float) height) / ((float) getHeight());
        width = Math.max(1, (int) (getWidth() * ratio));
    } else {
        if (height < 0) {
            float ratio = ((float) width) / ((float) getWidth());
            height = Math.max(1, (int) (getHeight() * ratio));
        }
    }
    try {
        ImageIO io = ImageIO.getImageIO();
        if (io != null) {
            String format = ImageIO.FORMAT_PNG;
            if (isOpaque() || !io.isFormatSupported(ImageIO.FORMAT_PNG)) {
                if (io.isFormatSupported(ImageIO.FORMAT_JPEG)) {
                    format = ImageIO.FORMAT_JPEG;
                }
            }
            if (io.isFormatSupported(format)) {
                // do an image IO scale which is more efficient
                ByteArrayOutputStream bo = new ByteArrayOutputStream();
                io.save(new ByteArrayInputStream(getImageData()), bo, format, width, height, 0.9f);
                EncodedImage img = EncodedImage.create(bo.toByteArray());
                Util.cleanup(bo);
                img.opaque = opaque;
                img.opaqueChecked = opaqueChecked;
                if (width > -1 && height > -1) {
                    img.width = width;
                    img.height = height;
                }
                return img;
            }
        }
    } catch (IOException err) {
        // normally this shouldn't happen but this will keep falling back to the existing scaled code
        Log.e(err);
    }
    return null;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ImageIO(com.codename1.ui.util.ImageIO)

Example 12 with EncodedImage

use of com.codename1.ui.EncodedImage in project CodenameOne by codenameone.

the class SignIn method showGoogleUser.

private void showGoogleUser(String token) {
    ConnectionRequest req = new ConnectionRequest();
    req.addRequestHeader("Authorization", "Bearer " + token);
    req.setUrl("https://www.googleapis.com/plus/v1/people/me");
    req.setPost(false);
    InfiniteProgress ip = new InfiniteProgress();
    Dialog d = ip.showInifiniteBlocking();
    NetworkManager.getInstance().addToQueueAndWait(req);
    d.dispose();
    byte[] data = req.getResponseData();
    JSONParser parser = new JSONParser();
    Map map = null;
    try {
        map = parser.parseJSON(new InputStreamReader(new ByteArrayInputStream(data), "UTF-8"));
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    String name = (String) map.get("displayName");
    Map im = (Map) map.get("image");
    String url = (String) im.get("url");
    Form userForm = new UserForm(name, (EncodedImage) theme.getImage("user.png"), url);
    userForm.show();
}
Also used : ConnectionRequest(com.codename1.io.ConnectionRequest) InputStreamReader(java.io.InputStreamReader) InfiniteProgress(com.codename1.components.InfiniteProgress) ByteArrayInputStream(java.io.ByteArrayInputStream) Form(com.codename1.ui.Form) Dialog(com.codename1.ui.Dialog) JSONParser(com.codename1.io.JSONParser) IOException(java.io.IOException) Map(java.util.Map)

Example 13 with EncodedImage

use of com.codename1.ui.EncodedImage in project CodenameOne by codenameone.

the class SignIn method start.

public void start() {
    // new SignInFormGB(theme).show();
    // if (true) return;
    loginForm = new Form("Sign in Demo");
    loginForm.setLayout(new BorderLayout());
    Container center = new Container(new BoxLayout(BoxLayout.Y_AXIS));
    center.setUIID("ContainerWithPadding");
    Image logo = theme.getImage("CodenameOne.png");
    Label l = new Label(logo);
    Container flow = new Container(new FlowLayout(Component.CENTER));
    flow.addComponent(l);
    center.addComponent(flow);
    final TextField username = new TextField();
    username.setHint("Username");
    final TextField pass = new TextField();
    pass.setHint("Password");
    pass.setConstraint(TextField.PASSWORD);
    center.addComponent(username);
    center.addComponent(pass);
    Button signIn = new Button("Sign In");
    signIn.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent evt) {
            if (username.getText().length() == 0 || pass.getText().length() == 0) {
                return;
            }
            UserForm userForm = new UserForm(username.getText(), (EncodedImage) theme.getImage("user.png"), null);
            userForm.show();
        }
    });
    center.addComponent(signIn);
    loginForm.addComponent(BorderLayout.CENTER, center);
    Container bottom = new Container(new BoxLayout(BoxLayout.Y_AXIS));
    Button loginWFace = new Button(theme.getImage("signin_facebook.png"));
    loginWFace.setUIID("LoginButton");
    loginWFace.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent evt) {
            // create your own app identity on facebook follow the guide here:
            // facebook-login.html
            String clientId = "1171134366245722";
            String redirectURI = "http://www.codenameone.com/";
            String clientSecret = "";
            if (clientSecret.length() == 0) {
                System.err.println("create your own facebook app follow the guide here:");
                System.err.println("http://www.codenameone.com/facebook-login.html");
                return;
            }
            Login fb = FacebookConnect.getInstance();
            fb.setClientId(clientId);
            fb.setRedirectURI(redirectURI);
            fb.setClientSecret(clientSecret);
            login = fb;
            fb.setCallback(new LoginListener(LoginListener.FACEBOOK));
            if (!fb.isUserLoggedIn()) {
                fb.doLogin();
            } else {
                showFacebookUser(fb.getAccessToken().getToken());
            }
        }
    });
    Button loginWG = new Button(theme.getImage("signin_google.png"));
    loginWG.setUIID("LoginButton");
    loginWG.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent evt) {
            SignIn.this.doFirebase();
            // create your own google project follow the guide here:
            // http://www.codenameone.com/google-login.html
            String clientId = "555462747934-iujpd5saj4pjpibo7c6r9tbjfef22rh1.apps.googleusercontent.com";
            String redirectURI = "https://www.codenameone.com/oauth2callback";
            String clientSecret = "650YqplrnAI0KXb9LMUnVNnx";
            if (clientSecret.length() == 0) {
                System.err.println("create your own google project follow the guide here:");
                System.err.println("http://www.codenameone.com/google-login.html");
                return;
            }
            Login gc = GoogleConnect.getInstance();
            gc.setClientId(clientId);
            gc.setRedirectURI(redirectURI);
            gc.setClientSecret(clientSecret);
            gc.setScope("https://www.googleapis.com/auth/plus.profile.emails.read https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/drive.file");
            login = gc;
            gc.setCallback(new LoginListener(LoginListener.GOOGLE));
            if (!gc.isUserLoggedIn()) {
                gc.doLogin();
            } else {
                showGoogleUser(gc.getAccessToken().getToken());
            }
        }
    });
    bottom.addComponent(loginWFace);
    bottom.addComponent(loginWG);
    loginForm.addComponent(BorderLayout.SOUTH, bottom);
    loginForm.show();
}
Also used : FlowLayout(com.codename1.ui.layouts.FlowLayout) Form(com.codename1.ui.Form) ActionEvent(com.codename1.ui.events.ActionEvent) BoxLayout(com.codename1.ui.layouts.BoxLayout) Label(com.codename1.ui.Label) Login(com.codename1.social.Login) EncodedImage(com.codename1.ui.EncodedImage) Image(com.codename1.ui.Image) EncodedImage(com.codename1.ui.EncodedImage) Container(com.codename1.ui.Container) BorderLayout(com.codename1.ui.layouts.BorderLayout) ActionListener(com.codename1.ui.events.ActionListener) Button(com.codename1.ui.Button) TextField(com.codename1.ui.TextField)

Example 14 with EncodedImage

use of com.codename1.ui.EncodedImage in project CodenameOne by codenameone.

the class SignIn method showFacebookUser.

private void showFacebookUser(String token) {
    ConnectionRequest req = new ConnectionRequest();
    req.setPost(false);
    req.setUrl("https://graph.facebook.com/v2.3/me");
    req.addArgumentNoEncoding("access_token", token);
    InfiniteProgress ip = new InfiniteProgress();
    Dialog d = ip.showInifiniteBlocking();
    NetworkManager.getInstance().addToQueueAndWait(req);
    byte[] data = req.getResponseData();
    JSONParser parser = new JSONParser();
    Map map = null;
    try {
        map = parser.parseJSON(new InputStreamReader(new ByteArrayInputStream(data), "UTF-8"));
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    String name = (String) map.get("name");
    d.dispose();
    Form userForm = new UserForm(name, (EncodedImage) theme.getImage("user.png"), "https://graph.facebook.com/v2.3/me/picture?access_token=" + token);
    userForm.show();
}
Also used : ConnectionRequest(com.codename1.io.ConnectionRequest) InputStreamReader(java.io.InputStreamReader) InfiniteProgress(com.codename1.components.InfiniteProgress) ByteArrayInputStream(java.io.ByteArrayInputStream) Form(com.codename1.ui.Form) Dialog(com.codename1.ui.Dialog) JSONParser(com.codename1.io.JSONParser) IOException(java.io.IOException) Map(java.util.Map)

Example 15 with EncodedImage

use of com.codename1.ui.EncodedImage in project codenameone-google-maps by codenameone.

the class MapContainer method addMarker.

/**
 * Adds a marker to the map with the given attributes
 * @param opts The marker options.
 * @return marker reference object that should be used when removing the marker
 */
public MapObject addMarker(MarkerOptions opts) {
    // public MapObject addMarker(EncodedImage icon, Coord location, String text, String longText, final ActionListener onClick) {
    EncodedImage icon = opts.getIcon();
    Coord location = opts.getLocation();
    String text = opts.getText();
    String longText = opts.getLongText();
    ActionListener onClick = opts.getOnClick();
    if (internalNative != null) {
        byte[] iconData = null;
        if (icon != null) {
            iconData = icon.getImageData();
            internalNative.setMarkerSize(icon.getWidth(), icon.getHeight());
        }
        long key = internalNative.addMarker(iconData, location.getLatitude(), location.getLongitude(), text, longText, onClick != null, opts.anchorU, opts.anchorV);
        MapObject o = new MapObject();
        o.mapKey = key;
        o.callback = onClick;
        markers.add(o);
        return o;
    } else {
        if (internalLightweightCmp != null) {
            PointLayer pl = new PointLayer(location, text, icon);
            if (points == null) {
                points = new PointsLayer();
                internalLightweightCmp.addLayer(points);
            }
            points.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent evt) {
                    PointLayer point = (PointLayer) evt.getSource();
                    for (MapObject o : markers) {
                        if (o.point == point) {
                            if (o.callback != null) {
                                o.callback.actionPerformed(new ActionEvent(o));
                            }
                            evt.consume();
                            return;
                        }
                    }
                }
            });
            points.addPoint(pl);
            MapObject o = new MapObject();
            o.point = pl;
            o.callback = onClick;
            markers.add(o);
            internalLightweightCmp.revalidate();
            return o;
        } else {
            String uri = null;
            int iconWidth = 0;
            int iconHeight = 0;
            int anchorU = 0;
            int anchorV = 0;
            if (icon != null) {
                uri = WebBrowser.createDataURI(icon.getImageData(), "image/png");
                iconWidth = icon.getWidth();
                iconHeight = icon.getHeight();
                anchorU = (int) (icon.getWidth() * opts.anchorU);
                anchorV = (int) (icon.getHeight() * opts.anchorV);
            }
            // browserBridge.waitForReady();
            MapObject o = new MapObject();
            o.callback = onClick;
            o.pending = true;
            final String fUri = uri;
            final int fIconWidth = iconWidth;
            final int fIconHeight = iconHeight;
            final float fAnchorU = anchorU;
            final float fAnchorV = anchorV;
            browserBridge.ready(() -> {
                internalBrowser.execute("callback.onSuccess(" + BRIDGE + ".addMarker(${0}, ${1}, ${2}, ${3}, ${4}, ${5}, ${6}, ${7}, ${8}))", new Object[] { // long key = ((Double)browserBridge.bridge.call("addMarker", new Object[]{
                fUri, location.getLatitude(), location.getLongitude(), text, longText, fIconWidth, fIconHeight, fAnchorU, fAnchorV }, jsres -> {
                    o.mapKey = jsres.getInt();
                    o.pending = false;
                });
            });
            // MapObject o = new MapObject();
            // o.mapKey = res.getInt();
            // o.callback = onClick;
            markers.add(o);
            // System.out.println("MapKey added "+o.mapKey);
            return o;
        }
    }
}
Also used : Coord(com.codename1.maps.Coord) PointsLayer(com.codename1.maps.layers.PointsLayer) ActionListener(com.codename1.ui.events.ActionListener) PointLayer(com.codename1.maps.layers.PointLayer) ActionEvent(com.codename1.ui.events.ActionEvent) EncodedImage(com.codename1.ui.EncodedImage) Point(com.codename1.ui.geom.Point)

Aggregations

EncodedImage (com.codename1.ui.EncodedImage)21 IOException (java.io.IOException)14 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 Image (com.codename1.ui.Image)6 AnimationObject (com.codename1.ui.animations.AnimationObject)5 BufferedImage (java.awt.image.BufferedImage)5 ByteArrayInputStream (java.io.ByteArrayInputStream)5 File (java.io.File)5 Hashtable (java.util.Hashtable)5 Map (java.util.Map)5 Form (com.codename1.ui.Form)4 ActionListener (com.codename1.ui.events.ActionListener)4 ImageIO (com.codename1.ui.util.ImageIO)4 ConnectionRequest (com.codename1.io.ConnectionRequest)3 ArrayList (java.util.ArrayList)3 InfiniteProgress (com.codename1.components.InfiniteProgress)2 MapObject (com.codename1.googlemaps.MapContainer.MapObject)2 Externalizable (com.codename1.io.Externalizable)2 JSONParser (com.codename1.io.JSONParser)2 Coord (com.codename1.maps.Coord)2