sábado, 2 de maio de 2015

Classe para verificar conexão com a Internet

public class Internet {

    public static boolean verificarConexao() {
        try {
            java.net.URL google = new java.net.URL("http://www.google.com");
            java.net.URLConnection conn = google.openConnection();
           

            java.net.HttpURLConnection httpConn = (java.net.HttpURLConnection) conn;
            httpConn.connect();
            int x = httpConn.getResponseCode();
            if (x == 200) {
                return true;
            }
        } catch (MalformedURLException ex) {
            Logger.getLogger(Internet.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        } catch (IOException ex) {
            Logger.getLogger(Internet.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
       
        return false;
    }

    public static boolean verificarConexao(String url) {
        try {
            java.net.URL teste = new java.net.URL(url);
            java.net.URLConnection conn = teste.openConnection();
           

            java.net.HttpURLConnection httpConn = (java.net.HttpURLConnection) conn;
            httpConn.connect();
            int x = httpConn.getResponseCode();
            if (x == 200) {
                return true;
            }
        } catch (MalformedURLException ex) {
            Logger.getLogger(Internet.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        } catch (IOException ex) {
            Logger.getLogger(Internet.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
       
        return false;
    }
}

Classe para exibir a Memória RAM do Sistema

public class MemoriaRAM {
    private static com.sun.management.OperatingSystemMXBean mxbean;
   
    static {
        mxbean = (com.sun.management.OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
    }
   
    public String getStringRAM(){
        String ram = getMemoriaLivreBytes() + "#";
        ram += getMemoriaLivreKBytes() + "#";
        ram += getMemoriaLivreMBytes() + "#";
        ram += getMemoriaLivreGBytes();
        return ram;
    }
   
    public static long getMemoriaTotalBytes(){
        return mxbean.getTotalPhysicalMemorySize();
    }
   
    public static long getMemoriaTotalKBytes(){
        return mxbean.getTotalPhysicalMemorySize()/1024;
    }
   
    public static long getMemoriaTotalMBytes(){
        return Math.round(mxbean.getTotalPhysicalMemorySize()/1024/1024);
    }
   
    public static long getMemoriaTotalGBytes(){
        return Math.round(mxbean.getTotalPhysicalMemorySize()/1024/1024/1024);
    }
   
    public static long getMemoriaLivreBytes(){
        return mxbean.getFreePhysicalMemorySize();
    }
   
    public static long getMemoriaLivreKBytes(){
        return mxbean.getFreePhysicalMemorySize()/1024;
    }
   
    public static long getMemoriaLivreMBytes(){
        return mxbean.getFreePhysicalMemorySize()/1024/1024;
    }
   
    public static long getMemoriaLivreGBytes(){
        return mxbean.getFreePhysicalMemorySize()/1024/1024/1024;
    }

    public static OperatingSystemMXBean getMxbean() {
        return mxbean;
    }
}

sábado, 25 de abril de 2015

Classe para enviar imagem pela rede


/**
 *
 * @author marcelo
 */
public class BufferedImageSeriolizable implements Serializable {
    private int width;
    private int height;
    private int[] pixels;

    public BufferedImageSeriolizable() {
    }

    public BufferedImageSeriolizable(File file) throws IOException{
        this(ImageIO.read(file));
    }
    
    public BufferedImageSeriolizable(BufferedImage bi) {
        //System.out.println("criando BufferedImageSeriolizable");
        width = bi.getWidth();
        height = bi.getHeight();
        pixels = new int[width * height];
        /*int[] tmp = */bi.getRGB(0, 0, width, height, pixels, 0, width);
    }

    public void setBufferedImage(BufferedImage bi){
        width = bi.getWidth();
        height = bi.getHeight();
        pixels = new int[width * height];
        bi.getRGB(0, 0, width, height, pixels, 0, width);
    }
    
    public BufferedImage getBufferedImage() {
        //System.out.println("getBufferedImage");
        BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        bi.setRGB(0, 0, width, height, pixels, 0, width);
        //System.out.println("BufferedImage: " + bi);
        return bi;
    }
    
    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public int[] getPixels() {
        return pixels;
    }

    public void setPixels(int[] pixels) {
        this.pixels = pixels;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }
    
    @Override
    public String toString() {
        return "BufferedImageSeriolizable{" + "width=" + width + ", height=" + height + ", pixels=" + pixels + '}';
    }
}

Métodos para redimensionar Image e ImageIcon

//Redimensionar um ImageIcon
public static ImageIcon getImageIconScaled(ImageIcon img, int width, int heigth) {
        img.setImage(img.getImage().getScaledInstance(width, heigth, 100));
        return img;
}

//redimensionar um BufferedImage
public static BufferedImage resizeImage(BufferedImage image, int width, int height) {
        int type = 0;
        type = image.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : image.getType();
        BufferedImage resizedImage = new BufferedImage(width, height, type);
        Graphics2D g = resizedImage.createGraphics();
        g.drawImage(image, 0, 0, width, height, null);
        g.dispose();

        return resizedImage;
}

public static BufferedImage resizeImageAffine(BufferedImage image, int width, int height) {
        AffineTransform transform = AffineTransform.getScaleInstance(width, height);
        BufferedImage resizedImage = new BufferedImage(width, height, image.getType());
        Graphics2D g2d = resizedImage.createGraphics();
        g2d.drawImage(image, transform, null);
        g2d.dispose();

        return resizedImage;
}

PrintScreen com Java

Nessa classe java podemos tirar um print screen da tela e salvar em um arquivo PNG ou JPG

**
 *
 * @author marcelo
 */
public class PrintScreen {
    public enum TIPO_IMAGEM {PNG, JPG}
    private  static Robot robo;
    private static Rectangle screenSize;
   
    static{
        screenSize = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
        try {
            robo = new Robot();
        } catch (AWTException ex) {
            Logger.getLogger(PrintScreen.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static Robot getRobo() {
        return robo;
    }
   
    public static BufferedImage getPrintScreen(){
        //System.out.println("getPrintScreen");
        if(robo != null){
            BufferedImage buff = robo.createScreenCapture(screenSize);
        //System.out.println("buff:" + buff);
            return buff;
        }
       
        return null;
    }
   
    public static BufferedImage getPrintScreen(int x,int y, int largura, int altura){
        screenSize = new Rectangle(x, y, largura, altura);
       
        if(robo != null){
            BufferedImage buff = robo.createScreenCapture(screenSize);
            return buff;
        }
       
        return null;
    }
   
    public static void savePrintFile(BufferedImage bufferedImage, File fileScreen, TIPO_IMAGEM tipoImagem){
     
        try {
            ImageIO.write(bufferedImage, tipoImagem.name(), fileScreen);
        } catch (IOException ex) {
            Logger.getLogger(EstacaoINFO.class.getName()).log(Level.SEVERE, null, ex);
        }
   
    }
}