Week 7 Rohan/Kinish
Week 7
Curreny Conversion
Code uses currency API to convert rates
package com.nighthawk.spring_portfolio.controllers;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.HashMap;
import java.util.Map;
import org.json.*;
public class CurrencyConversion {
public Map<String, Double> convertFrom (String currency, Double amount) {
Map<String, Double> rates = getRatesByEuro();
Map<String, Double> ret = new HashMap<String, Double>();
for (String key : rates.keySet()) {
ret.put(key, rates.get(key) / rates.get(currency) * amount);
}
return ret;
}
public static Map<String, Double> getRatesByEuro () {
Map<String, Double> ret = new HashMap<String, Double>();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://currency-conversion-and-exchange-rates.p.rapidapi.com/latest"))
.header("X-RapidAPI-Key", "92a566eec7msh22a859e4fed0e95p1f3079jsn8ea0b6a705c0")
.header("X-RapidAPI-Host", "currency-conversion-and-exchange-rates.p.rapidapi.com")
.method("GET", HttpRequest.BodyPublishers.noBody())
.build();
HttpResponse<String> response;
try {
response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
} catch (Exception e) {
System.out.println("error");
return ret;
}
System.out.println(response.body());
JSONObject jsonString = new JSONObject(response.body());
JSONObject rates = jsonString.getJSONObject("rates");
for (String key: rates.keySet()) {
double rate = rates.getDouble(key);
ret.put(key, rate);
}
return ret;
}
}
This is our spring code to use this class
package com.nighthawk.spring_portfolio.controllers;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller // HTTP requests are handled as a controller, using the @Controller annotation
public class CurrencyConverter {
@GetMapping("/currencyconvert")
public String currencyconvert(@RequestParam(name="currency", required=false) String currency, @RequestParam(name="amount", required=false) Double amount, Model model) {
if (currency != null && amount != null) {
CurrencyConversion converter = new CurrencyConversion();
Map<String, Double> currencyResults = converter.convertFrom(currency, amount);
model.addAttribute("results", currencyResults);
}
return "currencyconvert";
}
}
Image ASCII/Grayscale
package com.nighthawk.spring_portfolio.controllers;
import java.awt.image.BufferedImage;
import java.awt.Color;
//actually converts images (TODO: impelement the functions)
public class ImageConversion {
public BufferedImage same (BufferedImage img) {
return img;
}
public BufferedImage grayscale (BufferedImage img) {
for (int y = 0; y < img.getHeight(); y++) {
for (int x = 0; x < img.getWidth(); x++) {
int pixel = img.getRGB(x,y);
Color color = new Color(pixel, true);
int red = color.getRed();
int blue = color.getBlue();
int green = color.getGreen();
int grayscale = ((red + blue + green)/3);
int rgb=new Color(grayscale,grayscale,grayscale).getRGB();
img.setRGB(x,y,rgb);
}
}
return img;
}
public BufferedImage redscale (BufferedImage img) {
for (int y = 0; y < img.getHeight(); y++) {
for (int x = 0; x < img.getWidth(); x++) {
int pixel = img.getRGB(x,y);
Color color = new Color(pixel, true);
int red = color.getRed();
int blue = color.getBlue();
int green = color.getGreen();
int redscale = ((red + blue + green)/3);
int rgb=new Color(redscale,0,0).getRGB();
img.setRGB(x,y,rgb);
}
}
return img;
}
public BufferedImage bluescale (BufferedImage img) {
for (int y = 0; y < img.getHeight(); y++) {
for (int x = 0; x < img.getWidth(); x++) {
int pixel = img.getRGB(x,y);
Color color = new Color(pixel, true);
int red = color.getRed();
int blue = color.getBlue();
int green = color.getGreen();
int bluescale = ((red + blue + green)/3);
int rgb=new Color(0,0,bluescale).getRGB();
img.setRGB(x,y,rgb);
}
}
return img;
}
public BufferedImage greenscale (BufferedImage img) {
for (int y = 0; y < img.getHeight(); y++) {
for (int x = 0; x < img.getWidth(); x++) {
int pixel = img.getRGB(x,y);
Color color = new Color(pixel, true);
int red = color.getRed();
int blue = color.getBlue();
int green = color.getGreen();
int greenscale = ((red + blue + green)/3);
int rgb=new Color(0,greenscale,0).getRGB();
img.setRGB(x,y,rgb);
}
}
return img;
}
public String ascii (BufferedImage img) {
String asciiString = "";
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)));
asciiString += asciiChar(pixVal);
}
asciiString += "\n";
}
return asciiString;
}
private 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 = "88";
} else if (g >= 60) {
str = "##";
} else {
str = "@@";
}
return str;
}
}
This code uses this class with springboot.
package com.nighthawk.spring_portfolio.controllers;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.UUID;
import javax.imageio.ImageIO;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
@Controller
public class ImageConverter {
@GetMapping("/imageconvert")
public String convertGet(Model model) {
return "imageconvert";
}
@PostMapping("/imageconvert")
public String convertPost(@RequestParam(name="type", required=true) String type, @RequestParam("image") MultipartFile multipartFile, Model model) {
BufferedImage imBuff;
try {
imBuff = ImageIO.read(multipartFile.getInputStream());
} catch (IOException e) {
System.err.println("error occured wow sad cry about it");
return "imageconvert";
}
BufferedImage output = imBuff;
ImageConversion converter = new ImageConversion();
if (type.equals("ascii")) {
model.addAttribute("ascii", converter.ascii(imBuff));
return "imageconvert";
} else if (type.equals("gray")) {
output = converter.grayscale(imBuff);
} else if (type.equals("red")) {
output = converter.redscale(imBuff);
} else if (type.equals("green")) {
output = converter.greenscale(imBuff);
} else if (type.equals("blue")) {
output = converter.bluescale(imBuff);
}
String uniqueID = UUID.randomUUID().toString();
File outputfile = new File("target/classes/static/images/" + uniqueID + ".png");
try {
outputfile.createNewFile();
if (output != null) ImageIO.write(output, "png", outputfile);
} catch (IOException e) {
return "imageconvert";
}
model.addAttribute("image", "images/"+uniqueID+".png");
return "imageconvert";
}
}