0

モバイルアプリケーションからのリクエスト属性から画像バイトを取得しようとしています。

アップストリームに送信される画像の形式は BitMap です。Base64 形式でエンコードされます。

Web アプリケーションでバイトを取得する方法は? モバイル アプリケーション リクエストからデータを取得するために使用している Web アプリケーションで、以下のコード スニペットを使用しています。私はコードに感銘を受けました。私が正しいか間違っているかはわかりません。

byte[] line             = null;

    int noOfBytesRead;
    StringBuffer content    = new StringBuffer();

    HashMap<String, Object> lResultMap  =   null ;

    try 
    {
        log.info("request getInputStream: " + request.getInputStream());
        ServletInputStream sis  = request.getInputStream();

        line                    = new byte[128];
        noOfBytesRead           = sis.readLine(line, 0, 128);
        log.info("noOfBytesRead :1 " + noOfBytesRead);

        if (noOfBytesRead < 3)
            return null;

        noOfBytesRead           = sis.readLine(line, 0, 128); // Reads the content disposition and form name and filename

        log.info("line :"+line+" adn the noOfBytesRead : 2 : "  +   noOfBytesRead);

        int numBytesToRead      = noOfBytesRead;
        int availableBytesToRead;

        while ((availableBytesToRead = sis.available()) > 0) 
        {
            byte[] bufferBytesRead;
            bufferBytesRead     = availableBytesToRead >= numBytesToRead ? new byte[numBytesToRead] : new byte[availableBytesToRead];
            sis.read(bufferBytesRead);
            content.append(new String(bufferBytesRead, Charset.forName("iso-8859-1")));
        }
        sis.close();
        log.info("The bytes is  : " +content.toString());
        byte[] image        =   null;
        image   =   content.toString().getBytes(Charset.forName("iso-8859-1")) ;
        log.info("image : "  +image);


        Base64Decoder decoder = new Base64Decoder();  
        byte[] imgBytes = decoder.decode(content.toString());
        log.info("imgBytes : decoded byted" + imgBytes);        
        lResultMap          =   new HashMap<String, Object>();

        lResultMap.put("content"    , imgBytes );
        log.info("content string buffer :  " +content);
        log.info("lResultMap : "  +lResultMap);
    } 
    catch (Exception e) 
    {
        e.printStackTrace();
        log.log(Level.SEVERE, e.getMessage(), e);
    }
4

1 に答える 1

0

フォーマットされていないため、コードから理解するのは難しいですが、これが私の簡単な解決策です

URL url = new URL(contentUrl);
        URLConnection conn = url.openConnection();
        InputStream inStr = conn.getInputStream();
        BufferedInputStream buffInStr = new BufferedInputStream(inStr);
        // Need to check the size of Byte Array Buffer.
        ByteArrayBuffer baf = new ByteArrayBuffer(500);
        int current = 0;
        while ((current = buffInStr.read()) != -1) {
            baf.append((byte) current);
        }
        byte[] array = baf.toByteArray();

GoogleAppEngineで

Image service APIは、サポートされている任意の画像ファイル形式のデータを受け入れます。このページによると、これらの形式には「JPEG、PNG、WEBP、GIF(アニメーションGIFを含む)、BMP、TIFF、ICO」が含まれます。Imageサービスは、新しい画像データを最初から作成する方法を提供していません。ただし、グラフィックライブラリを使用して、受け入れられているデータ形式の1つで画像を生成し、サービスを使用して別の形式に変換したり、変換したりすることができます。もちろん、グラフィックライブラリによっては、サービスを使用せずに、ライブラリから直接最終的な画像を取得できる場合があります。

画像を提供するには、使用しているデータ形式に適切なContent-Typeヘッダーを設定してから、画像データのバイトを応答の出力ストリームに書き込みます。

// byte[] pngData = ...
resp.addHeader("Content-Type", "image/png");
resp.getOutputStream().write(pngData);

ライブラリなしで画像データを生成してみたい場合は、BMP形式が最も簡単な場合があります。画像サービスを使用して、これをPNGに変換できます。

于 2013-03-08T18:35:01.013 に答える