2014-03-25 2 views
0

나는 뱀처럼 보이는 앱을 만들었습니다. 큰 빨간색 블록이 하나 있으며 작은 빨간색 블록이 화면의 어딘가에있는 작은 흰색 블록을 터치 할 때마다 작은 흰색 블록이 나타납니다. 그것은 약 30 초 동안 반복됩니다. 그러나 이제 사람들이 예를 들어 자신의 머리 그림을 찍어서 빨간색 블록으로 사용하게 만드는 방법을 알고 싶습니다. 그래서 당신의 얼굴은 하얀 블록을 먹어야합니다. 나는 조금 주위를 수색했고 Smosh Super Head Esploder X는 내가 원하는 것과 같은 기능을 가지고있는 것을 보았다. 도와주세요!사람들이 내 앱에서 사용하는 사진을 찍게하려면 어떻게해야합니까?

+0

He 무슨 대답이든지 여기에 올거야. 카메라를 사용하려면 사용 권한을 지정해야합니다. –

답변

0

이 옵션을 사용, 그것은 당신이 갤러리에서 또한 카메라에서 사진을 촬영하고 도움 ..

/*captureImageInitialization Method 
* to select image from gallery or pick through camera 
*/ 
private void captureImageInitialization() { 
    /** 
    * a selector dialog to display two image source options, from camera 
    * ‘Take from camera’ and from existing files ‘Select from gallery’ 
    */ 
    final String[] items = new String[] { "Take from camera","Select from gallery" }; 
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getBaseContext(),android.R.layout.select_dialog_item, items); 
    AlertDialog.Builder builder = new AlertDialog.Builder(getBaseContext()); 

    builder.setTitle("Select Image"); 
    builder.setAdapter(adapter, new DialogInterface.OnClickListener() { 
     /* 
     * (non-Javadoc) 
     * @see android.content.DialogInterface.OnClickListener#onClick(android.content.DialogInterface, int) 
     */ 
     public void onClick(DialogInterface dialog, int item) { // pick from 
                   // camera 
       if (item == 0){ 
       /** 
       * To take a photo from camera, pass intent action 
       * ‘MediaStore.ACTION_IMAGE_CAPTURE‘ to open the camera app. 
       */ 
       Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 

       /** 
       * Also specify the Uri to save the image on specified path 
       * and file name. Note that this Uri variable also used by 
       * gallery app to hold the selected image path. 
       */ 
       mImageCaptureUri = Uri.fromFile(new File(Environment 
         .getExternalStorageDirectory(), "tmp_avatar_" 
         + 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) { 
        e.printStackTrace(); 
        } 
       } else{ 
       // pick from file 
       /** 
       * To select an image from existing files, use 
       * Intent.createChooser to open image chooser. Android will 
       * automatically display a list of supported applications, 
       * such as image gallery or file manager. 
       */ 
       Intent intent = new Intent(); 
       intent.setType("image/*"); 
       intent.setAction(Intent.ACTION_GET_CONTENT); 
       startActivityForResult(Intent.createChooser(intent, 
         "Complete action using"), PICK_FROM_FILE); 
      } 
     } 
    }); 
    dialog = builder.create(); 
} 
/* 
* (non-Javadoc) 
* @see android.app.Fragment#onActivityResult(int, int, android.content.Intent) 
*/ 
@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (resultCode != Activity.RESULT_OK) 
     return; 
    switch (requestCode) { 

    case PICK_FROM_CAMERA: 
     /** 
     * After taking a picture, do the crop 
     */ 
     doCrop(); 
     break; 
    case PICK_FROM_FILE: 
     /** 
     * After selecting image from files, save the selected path 
     */ 
     mImageCaptureUri = data.getData(); 
     doCrop(); 
     break; 
    case CROP_FROM_CAMERA: 
     Bundle extras = data.getExtras(); 
     /** 
     * After cropping the image, get the bitmap of the cropped image and 
     * display it on imageview. 
     */ 
     if (extras != null) { 
      Bitmap photo = extras.getParcelable("data"); 
      imageofuser.setImageBitmap(photo); 
      } 
     File f = new File(mImageCaptureUri.getPath()); 
     /** 
     * Delete the temporary image 
     */ 
     if (f.exists()) 
      f.delete(); 
      break; 
     } 
} 
/*doCrop() method 
    * this method crop the selected image and set to imageview 
    */ 
    private void doCrop() { 
     final ArrayList<CropOption> cropOptions = new ArrayList<CropOption>(); 
     /** 
     * Open image crop app by starting an intent 
     * ‘com.android.camera.action.CROP‘. 
     */ 
     Intent intent = new Intent("com.android.camera.action.CROP"); 
     intent.setType("image/*"); 

     /** 
     * Check if there is image cropper app installed. 
     */ 
     List<ResolveInfo> list = getBaseContext().getPackageManager().queryIntentActivities(intent, 0); 
     int size = list.size(); 

     /** 
     * If there is no image cropper app, display warning message 
     */ 
     if (size == 0) { 
      Toast.makeText(getBaseContext(), "Can not find image crop app",Toast.LENGTH_SHORT).show(); 
      return; 
     } else { 
     /** 
     * Specify the image path, crop dimension and scale 
     */ 
     intent.setData(mImageCaptureUri); 
     intent.putExtra("outputX", 200); 
     intent.putExtra("outputY", 200); 
     intent.putExtra("aspectX", 1); 
     intent.putExtra("aspectY", 1); 
     intent.putExtra("scale", true); 
     intent.putExtra("return-data", true); 
     /** 
     * There is posibility when more than one image cropper app exist, 
     * so we have to check for it first. If there is only one app, open 
     * then app. 
     */ 
       if (size==1){ 
       Intent i = new Intent(intent); 
       ResolveInfo res = list.get(0); 
       i.setComponent(new ComponentName(res.activityInfo.packageName, 
        res.activityInfo.name)); 
       startActivityForResult(i, CROP_FROM_CAMERA); 
      } else { 
      /** 
      * If there are several app exist, create a custom chooser to 
      * let user selects the app. 
      */ 
      for(ResolveInfo res : list) { 
       final CropOption co = new CropOption(); 
       co.title = getBaseContext().getPackageManager().getApplicationLabel(
         res.activityInfo.applicationInfo); 
       co.icon =getBaseContext().getPackageManager().getApplicationIcon(
         res.activityInfo.applicationInfo); 
       co.appIntent = new Intent(intent); 
       co.appIntent.setComponent(new ComponentName(
           res.activityInfo.packageName, 
           res.activityInfo.name)); 
       cropOptions.add(co); 
      } 
      CropOptionAdapter adapter = new CropOptionAdapter(getBaseContext().getApplicationContext(), cropOptions); 

      AlertDialog.Builder builder = new AlertDialog.Builder(getBaseContext()); 
      builder.setTitle("Choose Crop App"); 
      builder.setAdapter(adapter, 
        new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int item) { 
          startActivityForResult(
            cropOptions.get(item).appIntent, 
            CROP_FROM_CAMERA); 
         } 
        }); 

      builder.setOnCancelListener(new DialogInterface.OnCancelListener() { 
       @Override 
       public void onCancel(DialogInterface dialog) { 

        if (mImageCaptureUri != null) {getBaseContext().getContentResolver().delete(mImageCaptureUri, null, 
           null); 
         mImageCaptureUri = null; 
        } 
       } 
      }); 
      AlertDialog alert = builder.create(); 
      alert.show(); 
     } 
    } 
} 

CropOptionAdapter됩니다

public class CropOptionAdapter extends ArrayAdapter<CropOption> { 
    private ArrayList<CropOption> mOptions; 
    private LayoutInflater mInflater; 

    public CropOptionAdapter(Context context, ArrayList<CropOption> options){ 
     super(context, R.layout.crop_selector); 
     mOptions = options; 
     mInflater = LayoutInflater.from(context); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup group) { 
     if(convertView==null) 
     convertView = mInflater.inflate(R.layout.crop_selector, null); 
     CropOption item = mOptions.get(position); 
     if(item != null){ 
     ((ImageView) convertView.findViewById(R.id.iv_icon)) 
        .setImageDrawable(item.icon); 
     ((TextView) convertView.findViewById(R.id.tv_name)) 
        .setText(item.title); 
     return convertView; 
     } 
     return null; 
     } 
    } 

    class CropOption { 
    public CharSequence title; 
    public Drawable icon; 
    public Intent appIntent; 
} 

cropselector.xml :

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:padding="10dp"> 

    <ImageView 
     android:id="@+id/iv_icon" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 

    <TextView 
     android:id="@+id/tv_name" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text=""/> 
</LinearLayout> 

그리고 이미지 자르기 옵션을 제공합니다.

관련 문제