在blackberry中重新缩放图像并写入重新缩放的图像文件
我正在使用以下代码调整文件大小并将其保存到blackberry设备中。图像缩放后,我尝试将图像文件写入设备。但它给出了相同的数据。图像的高度和宽度是一样的。我必须重新缩放图像文件。有人能帮我吗 类ResizeImage扩展MainScreen实现FieldChangeListener { 私有字符串路径=file:///SDCard/BlackBerry/pictures/test.jpg; 私人按钮场btn; 调整图像大小 { btn=新按钮字段写入文件; btn.setChangeListenerthis; addbtn; } public void字段changedfield字段,int上下文 { 如果字段==btn { 尝试 { InputStream InputStream=null; //获取文件连接 FileConnection FileConnection=FileConnection Connector.openpath; 如果fileConnection.exe存在 { inputStream=fileConnection.openInputStream; //字节数据[]=inputStream.toString.getBytes在blackberry中重新缩放图像并写入重新缩放的图像文件,blackberry,jde,Blackberry,Jde,我正在使用以下代码调整文件大小并将其保存到blackberry设备中。图像缩放后,我尝试将图像文件写入设备。但它给出了相同的数据。图像的高度和宽度是一样的。我必须重新缩放图像文件。有人能帮我吗 类ResizeImage扩展MainScreen实现FieldChangeListener { 私有字符串路径=file:///SDCard/BlackBerry/pictures/test.jpg; 私人按钮场btn; 调整图像大小 { btn=新按钮字段写入文件; btn.setChangeListe
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");
}
}
}在EncodedImage中查看getScaledHeight和getScaledWidth 这是一个肮脏的圈套 如果使用具有有效getHeight和getWidth的getBitmap剥离位图 然后,如果要将缩放后的图像保存为jpeg格式,则需要重新编码 e、 g AFAIK,5.0之前的版本,至少在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.