import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

HttpRequest request = HttpRequest.newBuilder()
		.uri(URI.create("https://free-nba.p.rapidapi.com/players?page=0&per_page=25"))
		.header("X-RapidAPI-Key", "bc9e5f20f9mshfceb3f679afd2b7p1960cdjsn87cb096651ab")
		.header("X-RapidAPI-Host", "free-nba.p.rapidapi.com")
		.method("GET", HttpRequest.BodyPublishers.noBody())
		.build();

        HttpResponse<String> response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());

        System.out.println(response.body());
{"data":[{"id":14,"first_name":"Ike","height_feet":null,"height_inches":null,"last_name":"Anigbogu","position":"C","team":{"id":12,"abbreviation":"IND","city":"Indiana","conference":"East","division":"Central","full_name":"Indiana Pacers","name":"Pacers"},"weight_pounds":null},{"id":25,"first_name":"Ron","height_feet":null,"height_inches":null,"last_name":"Baker","position":"G","team":{"id":20,"abbreviation":"NYK","city":"New York","conference":"East","division":"Atlantic","full_name":"New York Knicks","name":"Knicks"},"weight_pounds":null},{"id":47,"first_name":"Jabari","height_feet":null,"height_inches":null,"last_name":"Bird","position":"G","team":{"id":2,"abbreviation":"BOS","city":"Boston","conference":"East","division":"Atlantic","full_name":"Boston Celtics","name":"Celtics"},"weight_pounds":null},{"id":67,"first_name":"MarShon","height_feet":null,"height_inches":null,"last_name":"Brooks","position":"G","team":{"id":15,"abbreviation":"MEM","city":"Memphis","conference":"West","division":"Southwest","full_name":"Memphis Grizzlies","name":"Grizzlies"},"weight_pounds":null},{"id":71,"first_name":"Lorenzo","height_feet":null,"height_inches":null,"last_name":"Brown","position":"G","team":{"id":28,"abbreviation":"TOR","city":"Toronto","conference":"East","division":"Atlantic","full_name":"Toronto Raptors","name":"Raptors"},"weight_pounds":null},{"id":90,"first_name":"Omri","height_feet":null,"height_inches":null,"last_name":"Casspi","position":"F","team":{"id":15,"abbreviation":"MEM","city":"Memphis","conference":"West","division":"Southwest","full_name":"Memphis Grizzlies","name":"Grizzlies"},"weight_pounds":null},{"id":1,"first_name":"Alex","height_feet":6,"height_inches":6,"last_name":"Abrines","position":"G","team":{"id":21,"abbreviation":"OKC","city":"Oklahoma City","conference":"West","division":"Northwest","full_name":"Oklahoma City Thunder","name":"Thunder"},"weight_pounds":200},{"id":119,"first_name":"Tyler","height_feet":null,"height_inches":null,"last_name":"Davis","position":"C","team":{"id":21,"abbreviation":"OKC","city":"Oklahoma City","conference":"West","division":"Northwest","full_name":"Oklahoma City Thunder","name":"Thunder"},"weight_pounds":null},{"id":149,"first_name":"Keenan","height_feet":null,"height_inches":null,"last_name":"Evans","position":"G","team":{"id":9,"abbreviation":"DET","city":"Detroit","conference":"East","division":"Central","full_name":"Detroit Pistons","name":"Pistons"},"weight_pounds":null},{"id":179,"first_name":"Marcin","height_feet":null,"height_inches":null,"last_name":"Gortat","position":"C","team":{"id":13,"abbreviation":"LAC","city":"LA","conference":"West","division":"Pacific","full_name":"LA Clippers","name":"Clippers"},"weight_pounds":null},{"id":1593,"first_name":"Andrew","height_feet":null,"height_inches":null,"last_name":"Bogut","position":"F","team":{"id":10,"abbreviation":"GSW","city":"Golden State","conference":"West","division":"Pacific","full_name":"Golden State Warriors","name":"Warriors"},"weight_pounds":null},{"id":241,"first_name":"Amir","height_feet":6,"height_inches":9,"last_name":"Johnson","position":"C-F","team":{"id":23,"abbreviation":"PHI","city":"Philadelphia","conference":"East","division":"Atlantic","full_name":"Philadelphia 76ers","name":"76ers"},"weight_pounds":240},{"id":392,"first_name":"Malachi","height_feet":null,"height_inches":null,"last_name":"Richardson","position":"G","team":{"id":29,"abbreviation":"UTA","city":"Utah","conference":"West","division":"Northwest","full_name":"Utah Jazz","name":"Jazz"},"weight_pounds":null},{"id":281,"first_name":"Zach","height_feet":null,"height_inches":null,"last_name":"Lofton","position":"G","team":{"id":9,"abbreviation":"DET","city":"Detroit","conference":"East","division":"Central","full_name":"Detroit Pistons","name":"Pistons"},"weight_pounds":null},{"id":263,"first_name":"Kosta","height_feet":7,"height_inches":0,"last_name":"Koufos","position":"C","team":{"id":26,"abbreviation":"SAC","city":"Sacramento","conference":"West","division":"Pacific","full_name":"Sacramento Kings","name":"Kings"},"weight_pounds":245},{"id":382,"first_name":"Billy","height_feet":null,"height_inches":null,"last_name":"Preston","position":"F","team":{"id":6,"abbreviation":"CLE","city":"Cleveland","conference":"East","division":"Central","full_name":"Cleveland Cavaliers","name":"Cavaliers"},"weight_pounds":null},{"id":384,"first_name":"Zhou","height_feet":null,"height_inches":null,"last_name":"Qi","position":"F-C","team":{"id":11,"abbreviation":"HOU","city":"Houston","conference":"West","division":"Southwest","full_name":"Houston Rockets","name":"Rockets"},"weight_pounds":null},{"id":388,"first_name":"Zach","height_feet":null,"height_inches":null,"last_name":"Randolph","position":"F","team":{"id":26,"abbreviation":"SAC","city":"Sacramento","conference":"West","division":"Pacific","full_name":"Sacramento Kings","name":"Kings"},"weight_pounds":null},{"id":430,"first_name":"DJ","height_feet":null,"height_inches":null,"last_name":"Stephens","position":"G-F","team":{"id":15,"abbreviation":"MEM","city":"Memphis","conference":"West","division":"Southwest","full_name":"Memphis Grizzlies","name":"Grizzlies"},"weight_pounds":null},{"id":437,"first_name":"Milos","height_feet":null,"height_inches":null,"last_name":"Teodosic","position":"G","team":{"id":13,"abbreviation":"LAC","city":"LA","conference":"West","division":"Pacific","full_name":"LA Clippers","name":"Clippers"},"weight_pounds":null},{"id":448,"first_name":"Gary","height_feet":null,"height_inches":null,"last_name":"Trent Jr.","position":"G","team":{"id":25,"abbreviation":"POR","city":"Portland","conference":"West","division":"Northwest","full_name":"Portland Trail Blazers","name":"Trail Blazers"},"weight_pounds":null},{"id":494,"first_name":"Michael","height_feet":null,"height_inches":null,"last_name":"Smith","position":"","team":{"id":2,"abbreviation":"BOS","city":"Boston","conference":"East","division":"Atlantic","full_name":"Boston Celtics","name":"Celtics"},"weight_pounds":null},{"id":495,"first_name":"John","height_feet":null,"height_inches":null,"last_name":"Morton","position":"","team":{"id":6,"abbreviation":"CLE","city":"Cleveland","conference":"East","division":"Central","full_name":"Cleveland Cavaliers","name":"Cavaliers"},"weight_pounds":null},{"id":496,"first_name":"Howard","height_feet":null,"height_inches":null,"last_name":"Wright","position":"","team":{"id":1,"abbreviation":"ATL","city":"Atlanta","conference":"East","division":"Southeast","full_name":"Atlanta Hawks","name":"Hawks"},"weight_pounds":null},{"id":497,"first_name":"Michael","height_feet":null,"height_inches":null,"last_name":"Ansley","position":"","team":{"id":22,"abbreviation":"ORL","city":"Orlando","conference":"East","division":"Southeast","full_name":"Orlando Magic","name":"Magic"},"weight_pounds":null}],"meta":{"total_pages":151,"current_page":1,"next_page":2,"per_page":25,"total_count":3757}}

Answering Questions about API

Benefits of using an API?

  • More efficient
  • Easier to automate and integrate
  • Easier to personalize
  • Can add in programs/databases a lot easier than rewriting the entire code

Benefits of backend implementation

  • Automates server-side development (increases efficiency)
  • Easier to edit databases and servers stored

Arrays and Images

// Red Scale
import java.io.File;
import java.io.IOException;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;

public class Red{
  public static void main(String args[])throws IOException{
    BufferedImage img = null;
    //File imageFile = null;
    String name = "moore";
    String in = "images/";
    String out = "images/tmp/";
    String name2 = "redmoore";
    
    


    try{
      File imageFile = new File(in + name + ".jpg");
      img = ImageIO.read(imageFile);
    }catch(IOException e){
      System.out.println(e);
    }

    int width = img.getWidth();
    int height = img.getHeight();

    for(int y = 0; y < height; y++){
      for(int x = 0; x < width; x++){
        int p = img.getRGB(x, y);
  
        int a = (p >> 24) & 0xff;
        int r = (p >> 16) & 0xff;

        p = (a << 24) | (r << 16) | (0 << 8) | 0;

        img.setRGB(x, y, p);
      }
    }

    //write image
    try{
      ImageIO.write(img, "gif", new File(out + name2 + ".jpg") );
      System.out.println("Success");
    }catch(IOException e){
      System.out.println(e);
    }
  }//main() ends here
}//class ends here

Red.main(null)
javax.imageio.IIOException: Can't read input file!
---------------------------------------------------------------------------
java.lang.NullPointerException: null
	at Red.main(#20:1)
	at .(#22:3)
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.awt.Image;
import java.awt.Graphics2D;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import javax.imageio.stream.ImageOutputStream;
import javax.imageio.stream.ImageInputStream;
import javax.imageio.metadata.IIOMetadata;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.ImageReader;
import javax.imageio.ImageTypeSpecifier;

public class Pics {
    private final String inDir = "images/"; // location of images
    private final String outDir = "images/tmp/";  // location of created files
    private String inFile;
    private String resizedFile;
    private String asciiFile;
    private String ext;   // extension of file
    private long bytes;
    private int width;
    private int height;

    // Constructor obtains attributes of picture
    public Pics(String name, String ext) {
        this.ext = ext;
        this.inFile = this.inDir + name + "." + ext;
        this.resizedFile = this.outDir + name + "." + ext;
        this.asciiFile = this.outDir + name + ".txt";
        this.setStats();
    }

    
    // An image contains metadata, namely size, width, and height
    public void setStats() {
        BufferedImage img;
        try {
            Path path = Paths.get(this.inFile);
            this.bytes = Files.size(path);
            img = ImageIO.read(new File(this.inFile));
            this.width = img.getWidth();
            this.height = img.getHeight();
        } catch (IOException e) {
        }
    }

    // Console print of data
    public void printStats(String msg) {
        System.out.println(msg + ": " + this.bytes + " " + this.width + "x" + this.height + "  " + this.inFile);
    }

    // Convert scaled image into buffered image
    public static BufferedImage convertToBufferedImage(Image img) {

        // Create a buffered image with transparency
        BufferedImage bi = new BufferedImage(
                img.getWidth(null), img.getHeight(null),
                BufferedImage.TYPE_INT_ARGB);

        // magic?
        Graphics2D graphics2D = bi.createGraphics();
        graphics2D.drawImage(img, 0, 0, null);
        graphics2D.dispose();

        return bi;
    }
    
    // Scale or reduce to "scale" percentage provided
    public void resize(int scale) {
        BufferedImage img = null;
        Image resizedImg = null;  

        int width = (int) (this.width * (scale/100.0) + 0.5);
        int height = (int) (this.height * (scale/100.0) + 0.5);

        try {
            // read an image to BufferedImage for processing
            img = ImageIO.read(new File(this.inFile));  // set buffer of image data
            // create a new BufferedImage for drawing
            resizedImg = img.getScaledInstance(width, height, Image.SCALE_SMOOTH);
        } catch (IOException e) {
            return;
        }

        try {
            ImageIO.write(convertToBufferedImage(resizedImg), this.ext, new File(resizedFile));
        } catch (IOException e) {
            return;
        }
        
        this.inFile = this.resizedFile;  // use scaled file vs original file in Class
        this.setStats();
    }
    
    // convert every pixel to an ascii character (ratio does not seem correct)
    public void convertToAscii() {
        BufferedImage img = null;
        PrintWriter asciiPrt = null;
        FileWriter asciiWrt = null;

        try {
            File file = new File(this.asciiFile);
            Files.deleteIfExists(file.toPath()); 
        } catch (IOException e) {
            System.out.println("Delete File error: " + e);
        }

        try {
            asciiPrt = new PrintWriter(asciiWrt = new FileWriter(this.asciiFile, true));
        } catch (IOException e) {
            System.out.println("ASCII out file create error: " + e);
        }

        try {
            img = ImageIO.read(new File(this.inFile));
        } catch (IOException e) {
        }

        for (int i = 0; i < img.getHeight(); i++) {
            for (int j = 0; j < img.getWidth(); j++) {
                Color col = new Color(img.getRGB(j, i));
                double pixVal = (((col.getRed() * 0.30) + (col.getBlue() * 0.59) + (col
                        .getGreen() * 0.11)));
                try {
                    asciiPrt.print(asciiChar(pixVal));
                    asciiPrt.flush();
                    asciiWrt.flush();
                } catch (Exception ex) {
                }
            }
            try {
                asciiPrt.println("");
                asciiPrt.flush();
                asciiWrt.flush();
            } catch (Exception ex) {
            }
        }
    }

    // conversion table, there may be better out there ie https://www.billmongan.com/Ursinus-CS173-Fall2020/Labs/ASCIIArt
    public String asciiChar(double g) {
        String str = " ";
        if (g >= 240) {
            str = " ";
        } else if (g >= 210) {
            str = ".";
        } else if (g >= 190) {
            str = "*";
        } else if (g >= 170) {
            str = "+";
        } else if (g >= 120) {
            str = "^";
        } else if (g >= 110) {
            str = "&";
        } else if (g >= 80) {
            str = "8";
        } else if (g >= 60) {
            str = "#";
        } else {
            str = "@";
        }
        return str;
    }

    // tester/driver
    public static void main(String[] args) throws IOException {
        Pics moore = new Pics("moore", "jpg");
        moore.printStats("Original");
        moore.resize(33);
        moore.printStats("Scaled");
        moore.convertToAscii();
    }
}
Pics.main(null);