Rescale immagine e scrittura riscalato file di immagine in mora
-
30-09-2019 - |
Domanda
Sto usando il seguente codice per ridimensionare e salvare il file al dispositivo BlackBerry. Dopo scala dell'immagine provo a scrivere file di immagine in dispositivo. Ma dà gli stessi dati. (Altezza e larghezza dell'immagine sono gli stessi) .I devono fare riscalato immagine file.Can chiunque aiutare me ???
class ResizeImage estende MainScreen attrezzi FieldChangeListener
{
sentiero privato String = "file: ///SDCard/BlackBerry/pictures/test.jpg";
btn ButtonField privato;
ResizeImage ()
{
btn = new ButtonField ( "Write File");
btn.setChangeListener (questo);
aggiungi (BTN);
}
public void fieldChanged (campo Campo, int contesto)
{
if (campo == BTN)
{
provare
{
InputStream inputStream = null;
// ottiene file di connessione
FileConnection FileConnection = (FileConnection) Connector.open (percorso);
if (fileConnection.exists ())
{
inputStream = fileConnection.openInputStream ();
// byte di dati [] = inputStream.toString () GetBytes ();.
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int j = 0;
while((j=inputStream.read()) != -1) {
baos.write(j);
}
byte data[] = baos.toByteArray();
inputStream.close();
fileConnection.close();
WriteFile("file:///SDCard/BlackBerry/pictures/org_Image.jpg",data);
EncodedImage eImage = EncodedImage.createEncodedImage(data,0,data.length);
int scaleFactorX = Fixed32.div(Fixed32.toFP(eImage.getWidth()), Fixed32.toFP(80));
int scaleFactorY = Fixed32.div(Fixed32.toFP(eImage.getHeight()), Fixed32.toFP(80));
eImage=eImage.scaleImage32(scaleFactorX, scaleFactorY);
WriteFile("file:///SDCard/BlackBerry/pictures/resize.jpg",eImage.getData());
BitmapField bit=new BitmapField(eImage.getBitmap());
add(bit);
}
}
catch(Exception e)
{
System.out.println("Exception is ==> "+e.getMessage());
}
}
}
void WriteFile(String fileName,byte[] data)
{
FileConnection fconn = null;
try
{
fconn = (FileConnection) Connector.open(fileName,Connector.READ_WRITE);
}
catch (IOException e)
{
System.out.print("Error opening file");
}
if (fconn.exists())
try
{
fconn.delete();
}
catch (IOException e)
{
System.out.print("Error deleting file");
}
try
{
fconn.create();
}
catch (IOException e)
{
System.out.print("Error creating file");
}
OutputStream out = null;
try
{
out = fconn.openOutputStream();
}
catch (IOException e) {
System.out.print("Error opening output stream");
}
try
{
out.write(data);
}
catch (IOException e) {
System.out.print("Error writing to output stream");
}
try
{
fconn.close();
}
catch (IOException e) {
System.out.print("Error closing file");
}
}
}
Soluzione
Scopri getScaledHeight () e getScaledWidth () in EncodedImage.
E 'un trucco sporco RIM.
Se si sbucciare la bitmap con getBitmap () che avrà getHeight valido () e getWidth ().
Poi, se si desidera salvare l'immagine in scala in formato jpeg è necessario ri-codificare.
per es.
Bitmap scaledBMP = scaledEI.getBitmap();
JPEGEncodedImage finalJPEG = JPEGEncodedImage.encode(scaledBMP, 80); // int arg is quality
raw_media_bytes = finalJPEG.getData();
raw_length = finalJPEG.getLength();
raw_offset = finalJPEG.getOffset();
// don't forget to use the length and offset info, because getData() is
// not guaranteed to work by itself.
Per quanto ne so, pre-5.0, almeno non c'è nativo di scala in-jpeg w / o la conversione di bitmap.