简体   繁体   中英

Rotate a BMP image in java without libraries java

This code works, it reads a file in byte type and after assigning the image it creates a copy in the directory where the other part is located with a different name, I must do the same, create a new file, I just have to make it rotate on the X and Y axes as the final 180 degree image without creating a library to do the job. Can you help me with the code or madnar information Thank you!


public class BMPRotations {
   public static void main(String[] args) throws IOException {
       int contador=0;
       
       int datos_entrada[] = new int[921655];
       
       
       try {
           FileInputStream archivo_lectura = new FileInputStream("Ruta__picture.bmp");
           boolean final_ar = false;
           
           while(!final_ar) {
               int byte_entrada = archivo_lectura.read();
               
               if(byte_entrada!=-1)
                   
                   datos_entrada[contador]=byte_entrada;
               else
                   final_ar=true;
               
               //Muestra todos los bytes 
               //System.out.println(datos_entrada[contador]);
               
               contador++;
               
           }
           archivo_lectura.close();
           
       }catch(IOException e) {
           System.out.print("Error");
           
           
       }
       System.out.print("Bystes de la imagen: " + contador);
       
       crea_fichero(datos_entrada);
   }
   static void crea_fichero(int datos_nuevo_fichero[]) {
       try {
           FileOutputStream fichero_nuevo = new FileOutputStream("Ruta_picture.bmp");
           
           for(int i=0; i<datos_nuevo_fichero.length;i++) {
               fichero_nuevo.write(datos_nuevo_fichero[i]);
               
           }
           
           fichero_nuevo.close();
           
           
           
       }catch(IOException e) {
           System.out.println("Error ");
           
       }
       
       
   }   

Here is a reference image.

640X480 in 24-bit format

https://i.stack.imgur.com/pz4A4.png

This isn't a full answer but I hope it points you in right direction for what looks like homework.

What you have implemented so far is simply copying a file with hard-coded size 921655, and does not deal with an image - just any file. You could replace the entire program with:

File input = new File("Ruta__picture.bmp");
File output = new File("Ruta_picture.bmp");
Files.copy(input.toPath(), output.toPath(), StandardCopyOption.REPLACE_EXISTING);

To deal with images, look at javax.imageio.ImageIO class. This shows how to load any supported JDK image type and write it back:

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;

BufferedImage image = ImageIO.read(input);

// TODO: TRANSFORM "image" here
BufferedImage modified = image;

ImageIO.write(modified , "bmp", output);

Note that ImageIO.write supports other types such as "jpg".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM