Foros del Web » Programación para mayores de 30 ;) » Java »

Image a File

Estas en el tema de Image a File en el foro de Java en Foros del Web. Hola, he estado buscando por el foro, pero no he encontrado nada y estoy atascada, tengo una imagen en una variable Image y la quiero ...
  #1 (permalink)  
Antiguo 22/11/2011, 04:33
 
Fecha de Ingreso: julio-2007
Mensajes: 75
Antigüedad: 16 años, 9 meses
Puntos: 0
Image a File

Hola,
he estado buscando por el foro, pero no he encontrado nada y estoy atascada, tengo una imagen en una variable Image y la quiero pasar a File (en realidad a bytes[], pero creo que lo voy a tener que hacer por partes, primero a File y de File a bytes[]).

Cómo podría pasar la variable Image a File (es jpg).


Un saludo y gracias
  #2 (permalink)  
Antiguo 22/11/2011, 05:50
Avatar de FiruzzZ  
Fecha de Ingreso: diciembre-2007
Ubicación: en casa
Mensajes: 470
Antigüedad: 16 años, 4 meses
Puntos: 41
Respuesta: Image a File

Tan simple como googlear :
Java Image to File
Java File to byte[]


Un poco de InputStream, BufferedInpu..., un array y listo
__________________
BadProgrammerException!

Última edición por FiruzzZ; 22/11/2011 a las 05:57
  #3 (permalink)  
Antiguo 23/11/2011, 02:16
 
Fecha de Ingreso: julio-2007
Mensajes: 75
Antigüedad: 16 años, 9 meses
Puntos: 0
Respuesta: Image a File

Gracias FiruzzZ
  #4 (permalink)  
Antiguo 25/11/2011, 03:41
 
Fecha de Ingreso: julio-2007
Mensajes: 75
Antigüedad: 16 años, 9 meses
Puntos: 0
Respuesta: Image a File

Hola a todos,

Os pongo dos soluciones para pasar un campo Image a un bytes[]:

FORMA 1:

File f = new File(tarjeta.getPersona().getPathFicherofoto());
byte [] bytesFoto = new byte[(int) f.length()];

try {
bytesFoto = FileUtils.readFileToByteArray( f );
}
catch (Exception ex) {
log.error("ERROR al pasar la foto a bytes", ex);
}
byte[] fotografia = Base64.encodeBase64(bytesFoto);


FORMA 2:

byte[] fotografia = null;
File fotoFile = new File(tarjeta.getPersona().getPathFicherofoto());
FileInputStream is = new FileInputStream(fotoFile);

// Get the size of the file
long length = fotoFile.length();

// Create the byte array to hold the data
byte[] bytes = new byte[(int) length];

// Read in the bytes
int offset = 0;
int numRead = 0;
while (offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
offset += numRead;
}

// Ensure all the bytes have been read in

if (offset < bytes.length) {
log.error("No se puede completar la lectura de la foto:" + fotoFile.getName());
//throw new IOException("Could not completely read file " + fotoFile.getName());
}
// Close the input stream and return bytes
is.close();

fotografia = Base64.encodeBase64(bytes);

Etiquetas: file, image
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 08:11.