2017-12-11 11 views
1

私は写真を撮ることができるカメラアプリを作ったが、特定の(プリセット)幅/高さとアスペクト比で保存したい。あらかじめ設定されたアスペクト比と幅/高さの画像をトリミングして保存する

例:

Example crop image with margins

  • レッド:古い絵(ソース)
  • 緑:赤と緑の間に新しい画像
  • :私が試したマージン

次の(運がない):

BitmapFactory.Options bmOptions = new BitmapFactory.Options(); 
    bitmap = BitmapFactory.decodeFile(editPhotoFile.getAbsolutePath(), bmOptions); 
    bitmap = Bitmap.createScaledBitmap(bitmap, bitmap.getWidth(), bitmap.getHeight(), true); 
    Bitmap croppedBmp = Bitmap.createBitmap(bitmap, margin, margin, bitmap.getWidth() - margin, bitmap.getHeight() - margin); 

私は左端(最初のマージン)と上端(2番目のマージン)、および全幅と高さに一定量のピクセルをカットするためにマージンを試しました。働いていない。

Githubにはいくつかのライブラリがありますが、画像を選択して編集することができます。手動編集は必要ありません。作図する余白をあらかじめ設定しておきます。

また、スタックオーバーフローのとちょうどチュートリアルをグーグルか見つけることによって、ここに可能な解決策は、私に運を与えることはありません。 検索クエリ:

のJavaアンドロイドクロップ画像私を助けることができる

のJavaアンドロイドクロップ画像のチュートリアル

+0

ので、ユーザーが画像croping..portionを編集したくありませんか? – rafsanahmad007

+0

私は自分自身でイメージを切り抜きたいので、イメージは各デバイスで同じ比率になります。一部のデバイスには3:4センサーがあり、その他のデバイスには9:16または16:9などがあります。それが理由。 –

答えて

1

私もそこに着いています。何週間も苦労しましたが、私の脳はクリアされました:すべてはうまくいっていましたが、私は画像/画像をデバイスに保存していませんでした。

マージンが効いています。

明日この記事を私が見つけた解決策で更新/編集します。

更新:

// get String with path to file from other activity and make new File of it 
    File editPhotoFile = new File(globalFileString); 
    BitmapFactory.Options bmOptions = new BitmapFactory.Options(); 
    bitmap = BitmapFactory.decodeFile(photoFile.getAbsolutePath(), bmOptions); 

    // crop the bitmap with new margins: bitmap, left, top, width, height 
    Bitmap croppedBmp = Bitmap.createBitmap(bitmap, marginLeft, marginTop, bitmapHeight, bitmapWidth); 

    // save bitmap to new file 
    FileOutputStream out = null; 
    File mediaFile; 
    try { 
     mediaFile = new File(globalFileString); 
     out = new FileOutputStream(mediaFile); 
     croppedBmp.compress(Bitmap.CompressFormat.JPEG, 100, out); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } finally { 
     try { 
      if (out != null) { 
       out.close(); 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
関連する問題