Breaking News


Wednesday, 25 December 2013

ANDROID IMAGE CROP EXAMPLE

ANDROID IMAGE CROP EXAMPLE....

ANDROID IMAGE CROP EXAMPLE SO, YOU ARE UPLOAD IMAGE IN YOUR DEVICES GALLERY OR BY CAMERA  AND CROP IMAGE 

first create mainactivity.java

public class MainActivity extends Activity implements OnClickListener {
private Uri mImageCaptureUri;
private ImageView mImageView;
Button b;
String image,s;
ListView lv;
JSONArray ja;
private static final int PICK_FROM_CAMERA = 1;
private static final int CROP_FROM_CAMERA = 2;
private static final int PICK_FROM_FILE = 3;
//String str = "http://10.0.2.2/arati/create_product.php";
//String get = "http://10.0.2.2/arati/get_all_products.php";
//JSONParser jsonParser = new JSONParser();
//ArrayList<HashMap<String, String>> h;
//JsonParse jsonParse = new JsonParse();
//JSONParser jsonParser = new JSONParser();
SimpleAdapter a;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.main);
        
        
        b = (Button)findViewById(R.id.button1);
        lv = (ListView)findViewById(R.id.listView1);
        b.setOnClickListener(this);
       /* h = new ArrayList<HashMap<String, String>>();
JSONObject jsonObject = jsonParse.getJSONFromUrl(get);
try {
ja = jsonObject.getJSONArray("store");
for (int i = 0; i < ja.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
JSONObject object = ja.getJSONObject(i);
s = object.getString(image);
// ss = object.getString(id);
// String s1 = object.getString(birth);
// String s2 = object.getString(address);
byte[] rawImage = Base64.decode(s, Base64.DEFAULT);
Bitmap bmp = BitmapFactory.decodeByteArray(rawImage, 0, rawImage.length);
ImageView imageview = (ImageView) findViewById(R.id.imageView1);
imageview.setImageBitmap(bmp);
map.put("image", s);
// map.put("birth", s1);
// map.put("address", s2);
h.add(map);


}

}

catch (Exception e) {
// TODO: handle exception
}
a = new SimpleAdapter(getApplicationContext(), h, R.layout.image,
new String[] { image }, new int[] { R.id.imageView1 });

lv.setAdapter(a);*/
        final String [] items = new String [] {"Take from camera", "Select from gallery"};
ArrayAdapter<String> adapter = new ArrayAdapter<String> (this, android.R.layout.select_dialog_item,items);
AlertDialog.Builder builder = new AlertDialog.Builder(this);

builder.setTitle("Select Image");
builder.setAdapter( adapter, new DialogInterface.OnClickListener() {
public void onClick( DialogInterface dialog, int item ) { //pick from camera
if (item == 0) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

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
Intent intent = new Intent();

               intent.setType("image/*");
               intent.setAction(Intent.ACTION_GET_CONTENT);
               
               startActivityForResult(Intent.createChooser(intent, "Complete action using"), PICK_FROM_FILE);
}
}
} );

final AlertDialog dialog = builder.create();

Button button = (Button) findViewById(R.id.btn_crop);
mImageView = (ImageView) findViewById(R.id.iv_photo);

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.show();
}
});
    }
    
    @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
   if (resultCode != RESULT_OK) return;
  
   switch (requestCode) {
   case PICK_FROM_CAMERA:
    doCrop();
   
    break;
   
   case PICK_FROM_FILE: 
    mImageCaptureUri = data.getData();
   
    doCrop();
   
    break;    
   
   case CROP_FROM_CAMERA:    
       Bundle extras = data.getExtras();

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

       File f = new File(mImageCaptureUri.getPath());            
       
       if (f.exists()) f.delete();

       break;

   }
}
    
    private void doCrop() {
final ArrayList<CropOption> cropOptions = new ArrayList<CropOption>();
   
    Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setType("image/*");
        
        List<ResolveInfo> list = getPackageManager().queryIntentActivities( intent, 0 );
        
        int size = list.size();
        
        if (size == 0) {        
        Toast.makeText(this, "Can not find image crop app", Toast.LENGTH_SHORT).show();
       
            return;
        } else {
        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);
            
        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 {
       for (ResolveInfo res : list) {
        final CropOption co = new CropOption();
       
        co.title = getPackageManager().getApplicationLabel(res.activityInfo.applicationInfo);
        co.icon = 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(getApplicationContext(), cropOptions);
       
       AlertDialog.Builder builder = new AlertDialog.Builder(this);
       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 ) {
                   getContentResolver().delete(mImageCaptureUri, null, null );
                   mImageCaptureUri = null;
               }
           }
       } );
       
       AlertDialog alert = builder.create();
       
       alert.show();
        }
        }
}

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub

}

@Override
/*public void onClick(View v) {

image = mImageView.toString();
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("image", image));
JSONObject jobj1 = jsonParser.makeHttpRequest(str,
"POST", params);

// TODO Auto-generated method stub

}*/
}

second create cropoptionadapte.java


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, options);

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;
}

}

third create CropOption.java

import android.content.Intent;
import android.graphics.drawable.Drawable;

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

create main.xml file

<?xml version="1.0" encoding="utf-8"?>
<!-- 
/*
**
** /res/layout/main.xml
**
** Author: Lorensius W. L. T <lorenz@londatiga.net
**
*/
-->

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    
<TextView  
   android:layout_width="fill_parent" 
   android:layout_height="wrap_content"
   android:layout_marginTop="10dp"
   android:layout_marginBottom="10dp"
   android:gravity="center"
   android:textStyle="bold"
   android:textSize="17sp"
   android:text="How to select and crop image"/>
   
<Button 
android:id="@+id/btn_crop"
android:layout_width="match_parent"  
android:layout_height="wrap_content"
android:text="Select image" />

<ImageView
   android:id="@+id/iv_photo"
   android:layout_width="50dp"
   android:layout_height="50dp"
   android:layout_gravity="center_horizontal"
   android:layout_marginRight="120dp"
   android:layout_marginTop="10dp" />

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




</LinearLayout>

create crop_selector.xml file

<?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"
android:gravity="center_vertical">

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

Share This
Blogger
Facebook
Disqus

comments powered by Disqus

1 comment :

Subscribe
Labels
Popular Posts

Subscribe Via Email

About Us

THIS IS ANDROID AND JAVA FREE TUTORIAL AND ALL PROVIDE SOLUTION IN ANDROID AND JAVA INTERVIEW QUE AND ANS IS AVAILABLE IN MY BLOG AND ANY QUERY PLEASE CONTACT ME GO NOW CONTACT US PAGE.

Total Pageviews

© Android and Java solution All rights reserved | Designed By Fireandroids