2013-01-04 25 views
5

में फोटो प्राप्त करें मैं एक फोटो लेने के लिए सरल उदाहरण खोज रहा हूं, और इसे यूआरआई का उपयोग करके सहेज रहा हूं और छवि प्रसंस्करण के लिए फोटो पुनर्प्राप्त कर रहा हूं, मैंने बहुत कोशिश की है उदाहरण कोड, लेकिन उनमें से कोई भी आसानी से चला गया।एक फोटो कैसे लें, इसे सहेजें और एंड्रॉइड

क्या किसी के पास उदाहरण कोड है?

+2

आपने अभी क्या किया है? –

+0

इस प्रश्न के बारे में 100 से अधिक भिन्न विषय हैं ... बस इसकी खोज करें! – safari

उत्तर

-1

कृपया निम्नलिखित कोड का उपयोग करें।

package com.example.stackoverflow; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.math.BigInteger; 
import java.security.SecureRandom; 

import android.app.Activity; 
import android.content.Intent; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.net.Uri; 
import android.os.Bundle; 
import android.os.Environment; 
import android.provider.MediaStore; 
import android.view.View; 
import android.widget.Button; 
import android.widget.GridView; 
import android.widget.ImageView; 

public class MyCameraActivity extends Activity { 
    private static final int CAMERA_REQUEST = 1888; 



    static String str_Camera_Photo_ImagePath = ""; 
    private static File f; 
    private static int Take_Photo = 2; 
    private static String str_randomnumber = ""; 
    static String str_Camera_Photo_ImageName = ""; 
    public static String str_SaveFolderName; 
    private static File wallpaperDirectory; 
    Bitmap bitmap; 
    int storeposition = 0; 
    public static GridView gridview; 
    public static ImageView imageView; 



    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.ccccc); 
     this.imageView = (ImageView)this.findViewById(R.id.imageView1); 
     Button photoButton = (Button) this.findViewById(R.id.button1); 
     photoButton.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       str_SaveFolderName = Environment 
         .getExternalStorageDirectory() 
         .getAbsolutePath() 
         + "/rajeshsample"; 
       str_randomnumber = String.valueOf(nextSessionId()); 
       wallpaperDirectory = new File(str_SaveFolderName); 
       if (!wallpaperDirectory.exists()) 
        wallpaperDirectory.mkdirs(); 
       str_Camera_Photo_ImageName = str_randomnumber 
         + ".jpg"; 
       str_Camera_Photo_ImagePath = str_SaveFolderName 
         + "/" + str_randomnumber + ".jpg"; 
       System.err.println(" str_Camera_Photo_ImagePath " 
         + str_Camera_Photo_ImagePath); 
       f = new File(str_Camera_Photo_ImagePath); 
       startActivityForResult(new Intent(
         MediaStore.ACTION_IMAGE_CAPTURE).putExtra(
         MediaStore.EXTRA_OUTPUT, Uri.fromFile(f)), 
         Take_Photo); 
       System.err.println("f " + f); 
      } 
     }); 
    } 


    // used to create randon numbers 
    public String nextSessionId() { 
     SecureRandom random = new SecureRandom(); 
     return new BigInteger(130, random).toString(32); 
    } 


    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (requestCode == Take_Photo) { 
      String filePath = null; 

      filePath = str_Camera_Photo_ImagePath; 
      if (filePath != null) { 
       Bitmap faceView = (new_decode(new File(
           filePath))); // ========================> good 
               // lines 



       imageView.setImageBitmap(faceView); 

      } else { 
       bitmap = null; 
      } 
     } 
    } 

    public static Bitmap new_decode(File f) { 

     // decode image size 

     BitmapFactory.Options o = new BitmapFactory.Options(); 
     o.inJustDecodeBounds = true; 
     o.inDither = false; // Disable Dithering mode 

     o.inPurgeable = true; // Tell to gc that whether it needs free memory, 
           // the Bitmap can be cleared 

     o.inInputShareable = true; // Which kind of reference will be used to 
            // recover the Bitmap data after being 
            // clear, when it will be used in the future 
     try { 
      BitmapFactory.decodeStream(new FileInputStream(f), null, o); 
     } catch (FileNotFoundException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 

     // Find the correct scale value. It should be the power of 2. 
     final int REQUIRED_SIZE = 300; 
     int width_tmp = o.outWidth, height_tmp = o.outHeight; 
     int scale = 1; 
     while (true) { 
      if (width_tmp/1.5 < REQUIRED_SIZE && height_tmp/1.5 < REQUIRED_SIZE) 
       break; 
      width_tmp /= 1.5; 
      height_tmp /= 1.5; 
      scale *= 1.5; 
     } 

     // decode with inSampleSize 
     BitmapFactory.Options o2 = new BitmapFactory.Options(); 
     // o2.inSampleSize=scale; 
     o.inDither = false; // Disable Dithering mode 

     o.inPurgeable = true; // Tell to gc that whether it needs free memory, 
           // the Bitmap can be cleared 

     o.inInputShareable = true; // Which kind of reference will be used to 
            // recover the Bitmap data after being 
            // clear, when it will be used in the future 
     // return BitmapFactory.decodeStream(new FileInputStream(f), null, o2); 
     try { 

//   return BitmapFactory.decodeStream(new FileInputStream(f), null, 
//     null); 
      Bitmap bitmap= BitmapFactory.decodeStream(new FileInputStream(f), null, null); 
      System.out.println(" IW " + width_tmp); 
      System.out.println("IHH " + height_tmp);   
       int iW = width_tmp; 
       int iH = height_tmp; 

       return Bitmap.createScaledBitmap(bitmap, iW, iH, true); 

     } catch (OutOfMemoryError e) { 
      // TODO: handle exception 
      e.printStackTrace(); 
      // clearCache(); 

      // System.out.println("bitmap creating success"); 
      System.gc(); 
      return null; 
      // System.runFinalization(); 
      // Runtime.getRuntime().gc(); 
      // System.gc(); 
      // decodeFile(f); 
     } catch (FileNotFoundException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      return null; 
     } 

    } 

} 

एक्सएमएल:

<?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" > 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="photo" > 
    </Button> 

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/ic_action_search" > 
    </ImageView> 

</LinearLayout> 
9

एंड्रॉयड से कैमरा फोन करने के लिए कोड का उपयोग करें इस

protected static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 0; 

की तरह एक चर परिभाषित करते हैं।

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
imageUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(),"fname_" +   
         String.valueOf(System.currentTimeMillis()) + ".jpg")); 
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageUri); 
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE); 

और क्लास कॉलिंग में यह एक्टिविटी रिसेट फ़ंक्शन को ओवरराइड करता है और नीचे दिए गए कोड को दर्ज करता है।

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

    if (resultCode == RESULT_OK) { 
     if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) { 

      //use imageUri here to access the image 

      Bundle extras = data.getExtras(); 

      Log.e("URI",imageUri.toString()); 

      Bitmap bmp = (Bitmap) extras.get("data"); 

      // here you will get the image as bitmap 


     } 
      else if (resultCode == RESULT_CANCELED) { 
      Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT); 
      } 
     } 


    } 
+17

क्या आपको एहसास है कि आपके द्वारा प्रदान किए गए कोड के साथ, आप छवि को पूर्ण आकार में यूरी में सहेज रहे हैं, लेकिन, आप एक्स्ट्रा से कम आकार की छवि को पुनर्प्राप्त कर रहे हैं, और उस कम आकार की छवि से बिटमैप बना रहे हैं? – paulkayuk

+0

मैं पूरी तरह से यूरी से छवि को पुनः प्राप्त कर रहा हूं। क्या आप छवि को कम करना चाहते हैं –

+0

@paulkayuk मैं संकुचित एक के बजाय पूर्ण आकार की छवि छवि कैसे प्राप्त करूं? – onexf

3

मुझे एक ही समस्या थी।

मैं इंटरनेट से कुछ कोड का परीक्षण कर रहा था, और कोई भी नहीं मिला। फिर, मैंने डेवलपर.android से कुछ मूलभूत कोडों का अध्ययन किया। उसके बाद, मैंने दो अलग-अलग कोड मिश्रित किए और मेरा काम किया! ये रहा!

public class MainActivity extends AppCompatActivity { 

    static final int PICTURE_RESULT = 1; 
    String mCurrentPhotoPath; 
    ContentValues values; 
    private Uri file; 
    ImageView imageView; 
    Bitmap help1; 

    ThumbnailUtils thumbnail; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     imageView = (ImageView) findViewById(R.id.imageView); 
     values = new ContentValues(); 
    } 

    public void launch_camera(View v) { 
     // the intent is my camera 
     Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
     //getting uri of the file 
     file = Uri.fromFile(getFile()); 

     //Setting the file Uri to my photo 
     intent.putExtra(MediaStore.EXTRA_OUTPUT,file); 

     if(intent.resolveActivity(getPackageManager())!=null) 
     { 
      startActivityForResult(intent, PICTURE_RESULT); 
     } 
    } 

    //this method will create and return the path to the image file 
    private File getFile() { 
     File folder = Environment.getExternalStoragePublicDirectory("/From_camera/imagens");// the file path 

     //if it doesn't exist the folder will be created 
     if(!folder.exists()) 
     {folder.mkdir();} 

     String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
     String imageFileName = "JPEG_"+ timeStamp + "_"; 
     File image_file = null; 

     try { 
      image_file = File.createTempFile(imageFileName,".jpg",folder); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     mCurrentPhotoPath = image_file.getAbsolutePath(); 
     return image_file; 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if(requestCode == PICTURE_RESULT) { 
      if(resultCode == Activity.RESULT_OK) { 
       try { 
        help1 = MediaStore.Images.Media.getBitmap(getContentResolver(),file); 
        imageView.setImageBitmap(thumbnail.extractThumbnail(help1,help1.getWidth(),help1.getHeight())); 
       }catch (Exception e){ 
        e.printStackTrace(); 
       } 
      } 
     } 
    } 
} 

एक्सएमएल फाइल सिर्फ एक बटन और एक imageView है और मत भूलना अपने Android में घोषित करने के लिए अनुमतियाँ प्रकट:

<uses-feature android:name="android.hardware.camera" android:required="true" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
अधिक जानकारियां के लिए

एक सुझाव है: https://developer.android.com/training/camera/photobasics.html#TaskPhotoView https://www.youtube.com/watch?v=je9bdkdNQqg

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