【问题标题】:Image rescale and write rescaled image file in blackberry图像重新缩放并在黑莓中写入重新缩放的图像文件
【发布时间】:2011-02-21 05:45:21
【问题描述】:

我正在使用以下代码来调整文件大小并将其保存到黑莓设备中。图像缩放后,我尝试将图像文件写入设备。但它给出了相同的数据。 (图像的高度和宽度相同)。我必须制作重新缩放的图像文件。有人可以帮我吗???

类 ResizeImage 扩展 MainScreen 实现 FieldChangeListener { 私人字符串路径="file:///SDCard/BlackBerry/pictures/test.jpg"; 私人 ButtonField btn; 调整大小图像() { btn=new ButtonField("写入文件"); btn.setChangeListener(this); 添加(btn); } public void fieldChanged(Field field, int context) { 如果(字段 == btn) { 尝试 {
输入流 inputStream = null; //获取文件连接 FileConnection fileConnection = (FileConnection) Connector.open(path); if (fileConnection.exists()) { inputStream = fileConnection.openInputStream(); //字节数据[]=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");
        }
    }

}

【问题讨论】:

    标签: blackberry jde


    【解决方案1】:

    查看 EncodedImage 中的 getScaledHeight() 和 getScaledWidth()。

    这是一个肮脏的 RIM 技巧。

    如果您使用 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,至少 5.0 之前的版本没有原生 in-jpeg 缩放,不支持位图转换。

    【讨论】:

      猜你喜欢
      • 2020-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多