2014-11-07 39 views
0

我试图检索一个视图的位图,本质上是一个截图。我目前的执行去如下:检索位图的视图结果在裁剪图像

public Bitmap loadBitmapFromView(View v) 
{ 
    Bitmap b = Bitmap.createBitmap(v.getHeight(), v.getWidth(), Bitmap.Config.ARGB_8888); 
    Canvas c = new Canvas(b); 
    v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height); 
    v.draw(c); 

    return b; 
} 

然而,出于某种原因,我得到一些问题,获取整个视图。结果图像被裁剪,留下一些部分被切断。起初,我认为这是布局设置的问题,因为我已将宽度设置为fill_parent,但当我将其设置为300dp时,问题仍然存在。

回答

0

MY贡献 - >

[ACTIVITY MAIN]

  //ADD PACKAGE HERE 

      import android.app.Activity; 
      import android.content.ActivityNotFoundException; 
      import android.content.Intent; 
      import android.graphics.Bitmap; 
      import android.net.Uri; 
      import android.os.Bundle; 
      import android.provider.MediaStore; 
      import android.view.View; 
      import android.view.View.OnClickListener; 
      import android.widget.Button; 
      import android.widget.ImageView; 
      import android.widget.Toast; 

      /** 
      * ShootAndCropActivity demonstrates capturing and cropping camera images 
      * - user presses button to capture an image using the device camera 
      * - when they return with the captured image Uri, the app launches the crop action intent 
      * - on returning from the crop action, the app displays the cropped image 
      * 
      * Sue Smith 
      * Mobiletuts+ Tutorial: Capturing and Cropping an Image with the Android Camera 
      * July 2012 
      * 
      */ 
      public class ShootAndCropActivity extends Activity implements OnClickListener { 

       //keep track of camera capture intent 
       final int CAMERA_CAPTURE = 1; 
       //keep track of cropping intent 
       final int PIC_CROP = 2; 
       //captured picture uri 
       private Uri picUri; 

       /** Called when the activity is first created. */ 
       @Override 
       public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 

        //retrieve a reference to the UI button 
        Button captureBtn = (Button)findViewById(R.id.capture_btn); 
        //handle button clicks 
        captureBtn.setOnClickListener(this); 
       } 

       /** 
       * Click method to handle user pressing button to launch camera 
       */ 
       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){ 
          //display an error message 
          String errorMessage = "Whoops - your device doesn't support capturing images!"; 
          Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT); 
          toast.show(); 
         } 
        } 
       } 

       /** 
       * Handle user returning from both capturing and cropping the image 
       */ 
       protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
        if (resultCode == RESULT_OK) { 
         //user is returning from capturing an image using the camera 
         if(requestCode == CAMERA_CAPTURE){ 
          //get the Uri for the captured image 
          picUri = data.getData(); 
          //carry out the crop operation 
          performCrop(); 
         } 
         //user is returning from cropping the image 
         else if(requestCode == PIC_CROP){ 
          //get the returned data 
          Bundle extras = data.getExtras(); 
          //get the cropped bitmap 
          Bitmap thePic = extras.getParcelable("data"); 
          //retrieve a reference to the ImageView 
          ImageView picView = (ImageView)findViewById(R.id.picture); 
          //display the returned cropped image 
          picView.setImageBitmap(thePic); 
         } 
        } 
       } 

       /** 
       * Helper method to carry out 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", 1); 
         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, PIC_CROP); 
        } 
        //respond to users whose devices do not support the crop action 
        catch(ActivityNotFoundException anfe){ 
         //display an error message 
         String errorMessage = "Whoops - your device doesn't support the crop action!"; 
         Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT); 
         toast.show(); 
        } 
       } 
      } 

[main.xml中]

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="vertical" > 

     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/intro" 
      android:layout_margin="3dp" 
      android:textStyle="bold" /> 
     <Button 
     android:id="@+id/capture_btn" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/capture" 
     /> 
     <ImageView 
      android:id="@+id/picture" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:contentDescription="@string/picture" 
     android:layout_margin="5dp" 
     android:background="@drawable/pic_border" /> 

    </LinearLayout>