题
我正在使用以下代码调整大小并将文件保存到BlackBerry设备中。图像刻度之后,我尝试将图像文件写入设备。但是它提供了相同的数据。 (图像的高度和宽度是相同的)。我必须制作恢复的图像文件。有人可以帮助我吗???
class resizeImage扩展了mainscreen实施fieldchangelistener {private string path =“ file:///sdcard/blackberry/pictures/pictures/test.jpg”;私人Buttonfield BTN; resizeImage(){btn = new buttonfield(“ write file”); btn.setchangelistener(this);添加(BTN); } public void fieldchanged(field field,int context){if(field == btn){尝试{
InputStream InputStream = null; //获取文件连接fileConnection fileConnection =(fileConnection)connector.open(path); if(fileconnection.exists()){inputStream = fileconnection.openinputStream(); //字节data [] = 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");
}
}
}
解决方案
在编码图中查看getScaledHeight()和getScaledWidth()。
这是一个肮脏的边缘技巧。
如果将getBitMap()剥离,将具有有效的Getheight()和getWidth()。
然后,如果要将缩放图像保存为JPEG,则需要重新编写。
例如
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.
Afaik,至少没有天然的jpeg缩放量表,没有转换为位图。