2013-10-29 5 views
8

मुझे कैमरे के इरादे (या कैमरा एपीआई) का उपयोग करके छवि लेने के बारे में एक सवाल है और फिर मेरे आवेदन में प्रदर्शित करने के लिए छवि को एक छवि दृश्य में लाएं। अभी तक मेरे पास इतना ही है।फोटो डब्ल्यू/कैमरा इरादा लें और छवि में देखें व्यू या टेक्स्ट व्यू?

मैं सेटअप एक बटन

Button btnPicture = (Button) findViewById(R.id.btn_picture); 
    btnPicture.setOnClickListener(this); 

मैं सेटअप एक कैमरा विधि

private void Camera() { 
    Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
    startActivityForResult(intent, TAKE_PICTURE_CODE); 
    intent.setAction(Intent.ACTION_GET_CONTENT); 
    intent.addCategory(Intent.CATEGORY_OPENABLE); 
    startActivityForResult(intent, REQUEST_CODE); 
} 

और यह मैं कहाँ खो रहा हूँ। मैं उस छवि को संसाधित करने की कोशिश कर रहा हूं जिसे मैंने लिया था।

private void processImage(Intent intent) { 
    setContentView(R.layout.imagelayout); 
    ImageView imageView = (ImageView)findViewById(R.id.image_view); 
    cameraBitmap = (Bitmap)intent.getExtras().get("data"); 
    imageView.setImageBitmap(cameraBitmap); 
} 

मेरे इरादे छवि है कि आप image_view अंदर ले लिया प्रदर्शित करने के लिए है। मुझे कोई त्रुटि नहीं मिल रही है, कुछ भी नहीं होता है। जब मैं तस्वीर लेता हूं, तो मुझे या तो एक और तस्वीर लेने के लिए कहा जाता है या जब मैं डिवाइस बैक बटन का उपयोग करता हूं तो एप्लिकेशन बल बंद हो जाता है। ऐसा लगता है कि मुझे अपने आवेदन से पूरी तरह से बाहर निकाला गया है, और लौटना एक बड़ा मुद्दा है। कोई सुझाव? मैं क्या खो रहा हूँ?

हे हां, और यहाँ

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
     if(TAKE_PICTURE_CODE == requestCode) { 

     Bundle extras = data.getExtras(); 
     if (extras.containsKey("data")) { 
      Bitmap bmp = (Bitmap) extras.get("data"); 
      ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
      bmp.compress(Bitmap.CompressFormat.PNG, 100, baos); 
      byte[] image = baos.toByteArray(); 
      if (image != null) { 
       Log.d(TAG, "image != null"); 
      } 

     } else { 
      Toast.makeText(getBaseContext(), "Fail to capture image", Toast.LENGTH_LONG).show(); 
     } 

    } 
} 

मैं getExtras में छवि डाल दिया, और फिर एक bytearray को संग्रहीत करने के लिए कोशिश कर रहा हूँ मेरी onActivityResult है। एक और चीज मैं करने की कोशिश कर रहा था। सुनिश्चित नहीं है कि यह सब कैसे एक साथ आता है।

+1

'atActivityResult() ' –

उत्तर

14

विधि है जो मैं आसान और उपयोगी हो पाया यह है:

MainActivity

private static String root = null; 
private static String imageFolderPath = null;   
private String imageName = null; 
private static Uri fileUri = null; 
private static final int CAMERA_IMAGE_REQUEST=1; 

public void captureImage(View view) { 

    ImageView imageView = (ImageView) findViewById(R.id.capturedImageview); 

      // fetching the root directory 
    root = Environment.getExternalStorageDirectory().toString() 
    + "/Your_Folder"; 

    // Creating folders for Image 
    imageFolderPath = root + "/saved_images"; 
    File imagesFolder = new File(imageFolderPath); 
    imagesFolder.mkdirs(); 

    // Generating file name 
    imageName = "test.png"; 

    // Creating image here 

    File image = new File(imageFolderPath, imageName); 

    fileUri = Uri.fromFile(image); 

    imageView.setTag(imageFolderPath + File.separator + imageName); 

    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 

    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); 

    startActivityForResult(takePictureIntent, 
      CAMERA_IMAGE_REQUEST); 

} 

और उसके बाद अपनी गतिविधि में onActivityResult विधि:

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    // TODO Auto-generated method stub 
    super.onActivityResult(requestCode, resultCode, data); 

    if (resultCode == RESULT_OK) { 

     switch (requestCode) { 
     case CAMERA_IMAGE_REQUEST: 

      Bitmap bitmap = null; 
      try { 
       GetImageThumbnail getImageThumbnail = new GetImageThumbnail(); 
       bitmap = getImageThumbnail.getThumbnail(fileUri, this); 
      } catch (FileNotFoundException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } catch (IOException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } 

      // Setting image image icon on the imageview 

      ImageView imageView = (ImageView) this 
        .findViewById(R.id.capturedImageview); 

      imageView.setImageBitmap(bitmap); 

      break; 

     default: 
      Toast.makeText(this, "Something went wrong...", 
        Toast.LENGTH_SHORT).show(); 
      break; 
     } 

    } 
} 

GetImageThumbnail.java

public class GetImageThumbnail { 

private static int getPowerOfTwoForSampleRatio(double ratio) { 
    int k = Integer.highestOneBit((int) Math.floor(ratio)); 
    if (k == 0) 
     return 1; 
    else 
     return k; 
} 

public Bitmap getThumbnail(Uri uri, Context context) 
     throws FileNotFoundException, IOException { 
    InputStream input = context.getContentResolver().openInputStream(uri); 

    BitmapFactory.Options onlyBoundsOptions = new BitmapFactory.Options(); 
    onlyBoundsOptions.inJustDecodeBounds = true; 
    onlyBoundsOptions.inDither = true;// optional 
    onlyBoundsOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;// optional 
    BitmapFactory.decodeStream(input, null, onlyBoundsOptions); 
    input.close(); 
    if ((onlyBoundsOptions.outWidth == -1) 
      || (onlyBoundsOptions.outHeight == -1)) 
     return null; 

    int originalSize = (onlyBoundsOptions.outHeight > onlyBoundsOptions.outWidth) ? onlyBoundsOptions.outHeight 
      : onlyBoundsOptions.outWidth; 

    double ratio = (originalSize > 400) ? (originalSize/350) : 1.0; 

    BitmapFactory.Options bitmapOptions = new BitmapFactory.Options(); 
    bitmapOptions.inSampleSize = getPowerOfTwoForSampleRatio(ratio); 
    bitmapOptions.inDither = true;// optional 
    bitmapOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;// optional 
    input = context.getContentResolver().openInputStream(uri); 
    Bitmap bitmap = BitmapFactory.decodeStream(input, null, bitmapOptions); 
    input.close(); 
    return bitmap; 
} 
} 

और इस तरह तो imageView पर onclick विधि हो जाएगा:

public void showFullImage(View view) { 
    String path = (String) view.getTag(); 

    if (path != null) { 

     Intent intent = new Intent(); 
     intent.setAction(Intent.ACTION_VIEW); 
     Uri imgUri = Uri.parse("file://" + path); 
     intent.setDataAndType(imgUri, "image/*"); 
     startActivity(intent); 

    } 

} 
+0

धन्यवाद, आपका उदाहरण व्यापक है लेकिन मदद मिली है। मैं बल का सामना कर रहा हूँ हालांकि बंद हो जाता है। जब मैं केवल BoundsOptions.inJustDecodeBounds लाइन से छुटकारा पाता हूं जो समस्या को ठीक करने के लिए प्रतीत होता है लेकिन चीजें अभी भी जिस तरह से काम करना चाहती हैं, काम नहीं कर रही हैं। मैं चारों ओर दिक्कत रखूंगा। लेकिन आपने मुझे प्रगति में मदद की है, धन्यवाद! – portfoliobuilder

+0

@portfoliobuilder आपका स्वागत है .. :) –

+0

यह काम करता है। थंबनेल() स्थिर बनाने पर विचार करें, आपको इसका उपयोग करने के लिए अपनी कक्षा के उदाहरण की आवश्यकता नहीं है। – lemuel

3

सही ढंग से फ़ोटो लेने के लिए आप, अस्थायी फ़ाइल में संग्रहीत करना चाहिए, क्योंकि परिणाम इरादे में डाटा शून्य हो सकता है:

final Intent pickIntent = new Intent(); 
pickIntent.setType("image/*"); 
pickIntent.setAction(Intent.ACTION_GET_CONTENT); 
final String pickTitle = activity.getString(R.string.choose_image); 
final Intent chooserIntent = Intent.createChooser(pickIntent, pickTitle); 
if (AvailabilityUtils.isExternalStorageReady()) { 
    final Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
    final Uri fileUri = getCameraTempFileUri(activity, true); 
    takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); 
    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, 
    new Intent[] { takePhotoIntent }); 
} 

activity.startActivityForResult(chooserIntent, REQUEST_CODE); 

और फिर उरी से तस्वीर मिलती है:

if (requestCode == ProfileDataView.REQUEST_CODE 
       && resultCode == Activity.RESULT_OK) { 
      final Uri dataUri = data == null ? getCameraTempFileUri(context, 
       false) : data.getData(); 
       final ParcelFileDescriptor pfd = context.getContentResolver() 
        .openFileDescriptor(imageUri, "r"); 
       final FileDescriptor fd = pfd.getFileDescriptor(); 
       final Bitmap bitmap = BitmapFactory.decodeFileDescriptor(fd); 
     } 
+0

10x के लिए कोड पोस्ट करें, लेकिन आपको' getCameraTempFileUri()' भी पोस्ट करना चाहिए। – WindRider

संबंधित मुद्दे