2016-09-02 16 views
1

画像をキャプチャして、指定された名前の元の画像の代わりに切り取った画像を保存しようとしています。 現時点では、私は画像ビューで切り抜くことができますが、元の画像ではなく切り取った画像を保存する方法を知る必要があります。ここにコードがあります。画像をキャプチャして切り抜き画像を切り抜いて保存画像

final int CAMERA_CAPTURE = 1; 
final int CROP_PIC = 2; 
private Uri picUri; 

@Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Button captureBtn = (Button) findViewById(R.id.capture_btn); 
     captureBtn.setOnClickListener(this); 
    } 

    public void onClick(View v) { 
     if (v.getId() == R.id.capture_btn) { 
      try { 
       // use standard intent to capture an image 
       Intent captureIntent = new Intent(
         MediaStore.ACTION_IMAGE_CAPTURE); 
       // we will handle the returned data in onActivityResult 
       startActivityForResult(captureIntent, CAMERA_CAPTURE); 
      } catch (ActivityNotFoundException anfe) { 
       Toast toast = Toast.makeText(this, "This device doesn't support the crop action!", 
         Toast.LENGTH_SHORT); 
       toast.show(); 
      } 
     } 
    } 

    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (resultCode == RESULT_OK) { 
      if (requestCode == CAMERA_CAPTURE) { 
       // get the Uri for the captured image 
       picUri = data.getData(); 
       performCrop(); 
      } 
      // user is returning from cropping the image 
      else if (requestCode == CROP_PIC) { 
       // get the returned data 
       Bundle extras = data.getExtras(); 
       // get the cropped bitmap 
       Bitmap thePic = extras.getParcelable("data"); 
       ImageView picView = (ImageView) findViewById(R.id.picture); 
       picView.setImageBitmap(thePic); 
      } 
     } 
    } 

    /** 
    * this function does the crop operation. 
    */ 
    private void performCrop() { 
     // take care of exceptions 
     try { 
      // call the standard crop action intent (the user device may not 
      // support it) 
      Intent cropIntent = new Intent("com.android.camera.action.CROP"); 
      // indicate image type and Uri 
      cropIntent.setDataAndType(picUri, "image/*"); 
      // set crop properties 
      cropIntent.putExtra("crop", "true"); 
      // indicate aspect of desired crop 
      cropIntent.putExtra("aspectX", 2); 
      cropIntent.putExtra("aspectY", 1); 
      // indicate output X and Y 
      cropIntent.putExtra("outputX", 256); 
      cropIntent.putExtra("outputY", 256); 
      // retrieve data on return 
      cropIntent.putExtra("return-data", true); 
      // start the activity - we handle returning in onActivityResult 
      startActivityForResult(cropIntent, CROP_PIC); 
     } 
     // respond to users whose devices do not support the crop action 
     catch (ActivityNotFoundException anfe) { 
      Toast toast = Toast 
        .makeText(this, "This device doesn't support the crop action!", Toast.LENGTH_SHORT); 
      toast.show(); 
     } 
    } 

私はアンドロイドに新しいですし、私は本当に助けのいずれかの種類があまりにappriciatedされて、私はこれを行うことができる方法を知りたいです。

+4

(HTTPS [Androidのは、 'CROP'を' Intent'持っていません] ://commonsware.com/blog/2013/01/23/no-android-does-not-have-crop-intent.html)。多くの[Android用画像トリミングライブラリ](https://android-arsenal.com/tag/45)があります。 1つを使用してください。 – CommonsWare

答えて

0

そこで質問です:どのように時々、ビットマップを保存bitmap

を保存するには長い時間がかかります。私はあなたのアプリで迷惑なラグを避けるためにAsyncTaskを使うことをお勧めします。

public class saveFile extends AsyncTask<Void, Void, File>{ 
     @Override 
     protected void onPreExecute() { 

      super.onPreExecute(); 

      progressBar.setVisibility(View.VISIBLE);//Show user that app is working in backgrind 

     } 


     @Override 
     protected File doInBackground(Void... params) { 

      String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() + 
        "/CropedImage"; 

      File dir = new File(file_path); 

      if(!dir.exists()) 

      dir.mkdirs(); 

      String format = new SimpleDateFormat("yyyyMMddHHmmss", 
      java.util.Locale.getDefault()).format(new Date()); 

      File file = new File(dir, format + ".png"); 

      FileOutputStream fOut; 

      try { 

      fOut = new FileOutputStream(file); 

      thePic.compress(Bitmap.CompressFormat.PNG, 100, fOut); 

      fOut.flush(); 

      fOut.close(); 

      } 

      catch (Exception e) { 

      e.printStackTrace(); 

      } 


       return file; 
     } 

     @Override 
      protected void onPostExecute(File result) { 

      progressBar.setVisibility(View.INVISIBLE); 

      String respath=result.getPath().toString(); 
      Toast.makeText(MainActivity.this, "Saved at "+respath, Toast.LENGTH_LONG).show(); 

      MediaScannerConnection.scanFile(MainActivity.this, new String[] { result.getPath() }, new String[] { "image/jpeg" }, null);  





      } 
    } 

そして呼び出すAsyncTask:

new saveFile().execute(); 
Androidはあなたが作物のライブラリをダウンロードすることができます任意の作物の意図を持っていません
+0

どこでAsyncTaskを使用する必要がありますか?私は新しいクラスを作る必要があるのですか? – ABi

+0

いいえ、あなたのMainActivity.class内に追加してください。 – BooDoo

+0

あなたの提案したソリューションを自分のコードでうまく解説してください。私はあなたの提案されたコードに従う方法を理解していません。私はuriを取得して、そのuriをビットマップに読み込むようにしています。 – ABi

関連する問題