2

StackOverflowやその他の役立つWebサイトのリソースを使用して、カメラアプリケーションで撮影した画像をAndroid携帯にアップロードできるアプリケーションを作成することに成功しました。唯一の問題は、私が今持っている電話は非常に高品質の写真を撮るため、アップロードの待ち時間が長くなることです。

画像をjpegから低レート(小さいサイズまたはWebに適したサイズ)に変換する方法について読みましたが、現在使用しているコードは、キャプチャされた画像をバイトとして保存します(以下のコードを参照)。画像の品質を元の形式に戻す方法はありますか、それとも画像をjpegに変換して画質を下げてから、バイト形式に戻す方法を見つける必要がありますか?

これが私が扱っているコードスニペットです:

    if (Intent.ACTION_SEND.equals(action)) {

        if (extras.containsKey(Intent.EXTRA_STREAM)) {
            try {

                // Get resource path from intent callee
                Uri uri = (Uri) extras.getParcelable(Intent.EXTRA_STREAM);

                // Query gallery for camera picture via
                // Android ContentResolver interface
                ContentResolver cr = getContentResolver();
                InputStream is = cr.openInputStream(uri);
                // Get binary bytes for encode
                byte[] data = getBytesFromFile(is);

                // base 64 encode for text transmission (HTTP)
                int flags = 1;
                byte[] encoded_data = Base64.encode(data, flags);
                // byte[] encoded_data = Base64.encodeBase64(data);
                String image_str = new String(encoded_data); // convert to
                                                                // string

                ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

                nameValuePairs.add(new BasicNameValuePair("image",
                        image_str));

                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost(
                        "http://xxxxx.php");
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                HttpResponse response = httpclient.execute(httppost);
                String the_string_response = convertResponseToString(response);
                Toast.makeText(UploadImage.this,
                        "Response " + the_string_response,
                        Toast.LENGTH_LONG).show();
            } catch (Exception e) {
                Toast.makeText(UploadImage.this, "ERROR " + e.getMessage(),
                        Toast.LENGTH_LONG).show();
                System.out.println("Error in http connection "
                        + e.toString());
            }
        }
    }
}
4

2 に答える 2

4

Webアプリの場合、カメラが生成する5つ以上のMP画像は絶対に必要ありません。画像の解像度は画像サイズの主な要因であるため、BitmapFactoryクラスを使用してダウンサンプリングされたビットマップを作成することをお勧めします。

特に、BitmapFactory.decodeByteArray()を見て、ダウンサンプリングされたビットマップが必要であることを示すBitmapFactory.Optionsパラメーターを渡します。

// your bitmap data
byte[] rawBytes = .......... ;

// downsample factor
options.inSampleSize = 4;  // downsample factor (16 pixels -> 1 pixel)

// Decode bitmap with inSampleSize set
return BitmapFactory.decodeByteArray(rawBytes, 0, rawBytes.length, options);

詳細については、ビットマップを効率的に表示するためのAndroidトレーニングレッスンとBitmapFactoryのリファレンスをご覧ください。

http://developer.android.com/training/displaying-bitmaps/index.html

http://developer.android.com/reference/android/graphics/BitmapFactory.html

于 2012-12-10T21:48:53.327 に答える
2

デコーダーに画像をサブサンプリングするように指示し、小さいバージョンをメモリにロードするには、BitmapFactory.OptionsオブジェクトでinSampleSizeをtrueに設定します。たとえば、inSampleSizeが4でデコードされた解像度2048x1536の画像は、約512x384のビットマップを生成します。これをメモリにロードすると、フルイメージに12MBではなく0.75MBが使用されます(ARGB_8888のビットマップ構成を想定)。これを参照してください

http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

public   Bitmap decodeSampledBitmapFromResource(
  String pathName) {
int reqWidth,reqHeight;
reqWidth =Utils.getScreenWidth();
reqWidth = (reqWidth/5)*2;
reqHeight = reqWidth;
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
//  BitmapFactory.decodeStream(is, null, options);
BitmapFactory.decodeFile(pathName, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(pathName, options);
}

   public   int calculateInSampleSize(BitmapFactory.Options options,
  int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;

if (height > reqHeight || width > reqWidth) {
  if (width > height) {
    inSampleSize = Math.round((float) height / (float) reqHeight);
  } else {
    inSampleSize = Math.round((float) width / (float) reqWidth);
  }
}
return inSampleSize;
 }
于 2013-01-23T11:21:23.723 に答える