0

次のコードを使用して、ファイルのサイズを変更し、ブラックベリーデバイスに保存しています。画像スケール後、画像ファイルをデバイスに書き込もうとします。しかし、それは同じデータを提供します。(画像の高さと幅は同じです)再スケーリングされた画像ファイルを作成する必要があります。誰か助けてもらえますか?

クラスResizeImageはMainScreenを拡張しFieldChangeListenerを実装します{privateStringpath = "file:/// SDCard / BlackBerry / pictures / test.jpg"; プライベートButtonFieldbtn; ResizeImage(){btn = new ButtonField( "ファイルの書き込み"); btn.setChangeListener(this); add(btn); } public void fieldChanged(Field field、int context){if(field == btn){try {
InputStream inputStream = null; //ファイル接続を取得FileConnectionfileConnection=(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");
        }
    }

}

4

1 に答える 1

0

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 より前のバージョンでは、ビットマップに変換しないネイティブの in-jpeg スケーリングはありません。

于 2010-05-19T06:30:29.947 に答える