Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package colors;
- import java.io.BufferedReader;
- import java.io.FileInputStream;
- import java.io.InputStreamReader;
- import javax.imageio.stream.*;
- import javax.imageio.ImageIO;
- import java.util.*;
- import javax.imageio.ImageReader;
- import java.awt.image.BufferedImage;
- public class Colors {
- BufferedImage bi; // imagen
- public BufferedImage cargarImagen(String imagen) {
- try {
- FileInputStream inputStream = new FileInputStream(imagen);
- Iterator readers = ImageIO.getImageReadersBySuffix("bmp");
- ImageReader reader = (ImageReader) readers.next();
- ImageInputStream imageInputStream = ImageIO
- .createImageInputStream(inputStream);
- reader.setInput(imageInputStream, false);
- return reader.read(0);
- } catch (Exception e) {
- e.printStackTrace();
- }
- return null;
- }
- public int obtenerComponentePixel(int line, int column, int offset) {
- return (bi.getRGB(column, line) >> offset)&255;
- }
- public int obtenerOffset(String componente) {
- if (componente.equals("R")) {
- return 16;
- } else if (componente.equals("G")) {
- return 8;
- }
- return 0;
- }
- public int obtenerSumaLinea(int linea, String componente) {
- if (linea < 0 || linea >= bi.getHeight()) {
- return -1;
- }
- int offset = obtenerOffset(componente);
- int resultado = 0;
- for (int i=0; i<bi.getWidth(); i++) {
- resultado += obtenerComponentePixel(linea, i, offset);
- }
- return resultado;
- }
- public Colors(String ficheroImagen) {
- bi = cargarImagen(ficheroImagen);
- }
- public static void main(String[] args) {
- Colors colors = new Colors("trabaja.bmp");
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- try {
- while (br.ready()) {
- String datos = br.readLine().trim();
- if (!datos.isEmpty()) {
- String componente = datos.substring(0, 1);
- int linea = Integer.parseInt(datos.substring(1));
- System.out.println(colors.obtenerSumaLinea(linea, componente)+1);
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement