2009-12-29 4 views
22

Ich versuche, ein Bild zuzuschneiden nach der Einnahme nehmen, und mein Code ist wie folgt:Android: ein Bild zuschneiden, nachdem es mit Kamera mit einem festen Seitenverhältnis

private void doTakePhotoAction() { 

     Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
     intent.putExtra(MediaStore.EXTRA_OUTPUT, MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString()); 
     intent.putExtra("crop", "true"); 
     intent.putExtra("aspectX", 1); 
     intent.putExtra("aspectY", 1); 
     intent.putExtra("outputX", 96); 
     intent.putExtra("outputY", 96); 

     try { 
      intent.putExtra("return-data", true); 
      startActivityForResult(intent, PICK_FROM_CAMERA); 
     } catch (ActivityNotFoundException e) { 
      //Do nothing for now 
     } 
    } 

Mit dem obigen Code, ich Ich kann in den Crop-Modus wechseln und das Bild zuschneiden. Das 1: 1-Seitenverhältnis wird jedoch nicht erzwungen, und auch OutputX und OutputY werden nicht verwendet. Ich glaube, das liegt daran, dass die Absicht war, ein Foto zu machen, nicht zum Zuschneiden. Ich habe auch eine andere Methode getData() aus der Absicht geschrieben, und danach die folgenden verwenden:

Intent intent = new Intent("com.android.camera.action.CROP"); 
intent.setClassName("com.android.camera", "com.android.camera.CropImage"); 

Allerdings, wenn ich das tun, erhalte ich die folgenden Laufzeitfehler:

E/AndroidRuntime(14648): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.camera/com.android.camera.CropImage}: java.lang.NullPointerException 

Danke für die Hilfe! :)

Antwort

2

Haben Sie das versucht Intent (aber behalten die Ernte/Aspekt/Ausgabe/Return-Daten Extras, die Sie bereits haben)?

Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
intent.setType("image/*"); 

, die im Grunde ist das, was die Android contacts application tut, so wird es vielleicht nicht ganz Ihren Anwendungsfall passen (dh ein Foto sofort nehmen, anstatt die Möglichkeit, eine aus der Galerie auswählen oder ein neues Foto aufnehmen).

Einen Versuch wert, trotzdem! :)

+0

Funktioniert auch nicht für mich sehen Keine Aktivität gefunden Intent {act = android.intent.action.GET_CONTENT (hat Extras)} – stoefln

22

Nachdem ich etwas gelesen habe, merkte ich, dass es nicht so einfach gemacht werden kann. Meine modded Contacts Quelle ist unter http://github.com/Wysie, Sie können sich ansehen, wenn Sie interessiert sind. Auch hier ist das, was ich habe es zum Laufen zu bringen:

private void doTakePhotoAction() { 
    // http://2009.hfoss.org/Tutorial:Camera_and_Gallery_Demo 
    // http://stackoverflow.com/questions/1050297/how-to-get-the-url-of-the-captured-image 
    // http://www.damonkohler.com/2009/02/android-recipes.html 
    // http://www.firstclown.us/tag/android/ 
    // The one I used to get everything working: http://groups.google.com/group/android-developers/msg/2ab62c12ee99ba30 

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 

    //Wysie_Soh: Create path for temp file 
    mImageCaptureUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), 
         "tmp_contact_" + String.valueOf(System.currentTimeMillis()) + ".jpg")); 

    intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri); 

    try { 
     intent.putExtra("return-data", true); 
     startActivityForResult(intent, PICK_FROM_CAMERA); 
    } catch (ActivityNotFoundException e) { 
     //Do nothing for now 
    } 
} 

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (resultCode != RESULT_OK) { 
     return; 
    } 

    switch (requestCode) { 

    case CROP_FROM_CAMERA: { 
     //Wysie_Soh: After a picture is taken, it will go to PICK_FROM_CAMERA, which will then come here 
     //after the image is cropped. 

     final Bundle extras = data.getExtras(); 

     if (extras != null) { 
      Bitmap photo = extras.getParcelable("data"); 

      mPhoto = photo; 
      mPhotoChanged = true; 
      mPhotoImageView.setImageBitmap(photo); 
      setPhotoPresent(true); 
     } 

     //Wysie_Soh: Delete the temporary file       
     File f = new File(mImageCaptureUri.getPath());    
     if (f.exists()) { 
      f.delete(); 
     } 

     InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
     mgr.showSoftInput(mPhotoImageView, InputMethodManager.SHOW_IMPLICIT); 

     break; 
    } 

    case PICK_FROM_CAMERA: { 
     //Wysie_Soh: After an image is taken and saved to the location of mImageCaptureUri, come here 
     //and load the crop editor, with the necessary parameters (96x96, 1:1 ratio) 

     Intent intent = new Intent("com.android.camera.action.CROP"); 
     intent.setClassName("com.android.camera", "com.android.camera.CropImage"); 

     intent.setData(mImageCaptureUri); 
     intent.putExtra("outputX", 96); 
     intent.putExtra("outputY", 96); 
     intent.putExtra("aspectX", 1); 
     intent.putExtra("aspectY", 1); 
     intent.putExtra("scale", true); 
     intent.putExtra("return-data", true);    
     startActivityForResult(intent, CROP_FROM_CAMERA); 

     break; 

    } 
    } 
} 

Hoffe, es hilft :)

+0

genial zu handhaben! Vielen Dank! –

+16

Danke Wysie, aber es funktioniert nicht in Android 2.x. Dies ist die Ausnahme: 04-29 18: 35: 02.857: FEHLER/AndroidRuntime (3486): verursacht durch: android.content.ActivityNotFoundException: Die explizite Aktivitätsklasse konnte nicht gefunden werden {com.android.camera/com.android.camera .Bild zuschneiden}; Hast du diese Aktivität in deiner AndroidManifest.xml deklariert? –

+0

wenn outputX ist mehr als 500 so funktioniert keine Idee? – somish

3

Check out this post. Ich habe es auf meinem Android 1.5 (HTC Magic) getestet und perfekt funktioniert.

Android Works

-3

Obwohl dies könnte einen sehr alten Thread, ich war in der Lage ein Bild programmatisch mit dem folgenden Code zu beschneiden:

 btnTakePicture.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      Intent cameraIntent = new Intent(
        android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 

      startActivityForResult(cameraIntent, CAMERA_REQUEST); 
     } 
    }); 

dann abgeschnitten ich es mit:

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 

    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) { 

     photo = (Bitmap) data.getExtras().get("data"); 

     performcrop(); 
    } 

} 

private void performcrop() { 
    DisplayMetrics displayMetrics = this.getResources().getDisplayMetrics(); 
    int width = displayMetrics.widthPixels; 
    int height = displayMetrics.heightPixels; 

    Bitmap croppedBmp = Bitmap.createBitmap(photo, 0, 0, width/2, 
      photo.getHeight()); 

    imageTaken.setImageBitmap(croppedBmp); 
} 

imageTaken ist eine ImageView-Komponente aus meiner Sicht. android.content.ActivityNotFoundException: Sie können meine Quelle Here

Verwandte Themen