2016-09-02 2 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(); 
     } 
    } 

나는 안드로이드를 처음 접했고 나는 이것을 어떻게 할 수 있는지 정말로 알고 싶다. 어떤 종류의 도움이라도 많이 부 풀린다.

+4

(HTTPS [안드로이드는'CROP'을'Intent'이 없습니다] : //commonsware.com/blog/2013/01/23/no-android-does-not-have-crop-intent.html). 많은 [안드로이드에서 사용할 수있는 이미지 자르기 라이브러리] (https://android-arsenal.com/tag/45)가 있습니다. 하나를 사용하십시오. – 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(); 
안드로이드는 작물 라이브러리를 다운로드 할 수있는 작물 의도가없는
+0

어디에서 AsyncTask를 사용해야합니까? 나는 새로운 수업을 만들어야한다는 뜻인가요? – ABi

+0

아니요, 아니요, 그냥 MainActivity.class에 추가하십시오. – BooDoo

+0

제안 된 솔루션을 코드로 자세히 작성하십시오. 제안 된 코드를 따르는 방법을 모르겠다. 나는 uri를 가져오고 그 uri를 비트 맵으로 읽는 중입니다. – ABi

관련 문제