开发者

Image rescale and write rescaled image file in blackberry

开发者 https://www.devze.com 2022-12-30 14:56 出处:网络
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 ima

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 {

Input开发者_如何学PythonStream 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");
        }
    }

}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消