图像重新缩放并在黑莓中写入重新缩放的图像文件

发布于 2024-09-01 17:23:50 字数 3414 浏览 5 评论 0原文

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

ResizeImage 类扩展 MainScreen 实现 FieldChangeListener { 私有字符串路径=“文件:///SDCard/BlackBerry/pictures/test.jpg”; 私有 ButtonField btn; 调整图像大小() { btn=new ButtonField("写入文件"); btn.setChangeListener(this); 添加(按钮); } 公共无效fieldChanged(字段字段,int上下文) { 如果(字段== btn) { 尝试 {
输入流 输入流 = null; //获取文件连接 FileConnection fileConnection = (FileConnection) Connector.open(路径); 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");
        }
    }

}

I am using the following code to resize and save the file in to the blackberry device. After image scale I try to write image file into device. But it gives the same data. (Height and width of the image are same).I have to make rescaled image file.Can anyone help me ???

class ResizeImage extends MainScreen implements FieldChangeListener
{
private String path="file:///SDCard/BlackBerry/pictures/test.jpg";
private ButtonField btn;
ResizeImage()
{
btn=new ButtonField("Write File");
btn.setChangeListener(this);
add(btn);
}
public void fieldChanged(Field field, int context)
{
if (field == btn)
{
try
{
InputStream inputStream = null;
//Get File Connection
FileConnection fileConnection = (FileConnection) Connector.open(path);
if (fileConnection.exists())
{
inputStream = fileConnection.openInputStream();
//byte 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");
        }
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我恋#小黄人 2024-09-08 17:23:50

查看 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.

,据我所知,至少在 5.0 之前,没有本地 jpeg 缩放功能,无需转换为位图。

Check out getScaledHeight() and getScaledWidth() in EncodedImage.

It's a dirty RIM trick.

If you peel the bitmap out with getBitmap() that will have valid getHeight() and getWidth().

Then if you want to save that scaled image as a jpeg you need to re-encode.

e.g.

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, pre-5.0 at least there's no native in-jpeg scaling w/o converting to bitmap.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文