Android downx=upx; 绒毛=upy; 打破 case MotionEvent.ACTION\u UP: upx=event.getX(); upy=event.getY(); 帆布.抽绳(downx,downy,upx,upy,paint); clipPath.lineTo(upx,upy); drawImageView.invalidate(); cropImageByPath(); 打破 case MotionEvent.ACTION\u取消: 打破 违约: 打破 } 返回true; } 私有void cropImageByPath(){ //现在关闭路径。 clipPath.close(); //将填充类型设置为“反向”,以便填充选定路径的外部。 clipPath.setFillType(FillType.反向绕组); Paint xferPaint=新油漆(Paint.ANTI_别名_标志); xferPaint.setColor(颜色:黑色); 画布绘制路径(clipPath,xferPaint); xfirpaint.setXfermode(新的PorterDuffXfermode(PorterDuff.Mode.DST_-IN)); canvas.drawBitmap(alteredBitmap,0,0,xferPaint); } 私有void doCrop(){ 试一试{ sourceBitmap=MediaStore.Images.Media.getBitmap(this.getContentResolver(),mImageCaptureUri); Options bmpFactoryOptions=新的BitmapFactory.Options(); bmpFactoryOptions.inJustDecodeBounds=true; bmp=BitmapFactory.decodeStream(getContentResolver().openInputStream( mImageCaptureUri),null,bmpFactoryOptions); bmpFactoryOptions.inJustDecodeBounds=false; bmp=BitmapFactory.decodeStream(getContentResolver().openInputStream( mImageCaptureUri),null,bmpFactoryOptions); alteredBitmap=Bitmap.createBitmap(bmp.getWidth(),bmp .getHeight(),bmp.getConfig(); 画布=新画布(alteredBitmap); 油漆=新油漆(); 油漆。设置颜色(颜色。绿色); 油漆。设置行程宽度(5); 矩阵=新矩阵(); 画布.绘图位图(bmp、矩阵、绘画); //在图像视图中加载图像位图 drawImageView.setImageBitmap(alteredBitmap); //设置触摸式侦听器 drawImageView.setOnTouchListener(此); }catch(filenotfounde异常){ e、 printStackTrace(); }捕获(IOE异常){ e、 printStackTrace(); } } }

Android downx=upx; 绒毛=upy; 打破 case MotionEvent.ACTION\u UP: upx=event.getX(); upy=event.getY(); 帆布.抽绳(downx,downy,upx,upy,paint); clipPath.lineTo(upx,upy); drawImageView.invalidate(); cropImageByPath(); 打破 case MotionEvent.ACTION\u取消: 打破 违约: 打破 } 返回true; } 私有void cropImageByPath(){ //现在关闭路径。 clipPath.close(); //将填充类型设置为“反向”,以便填充选定路径的外部。 clipPath.setFillType(FillType.反向绕组); Paint xferPaint=新油漆(Paint.ANTI_别名_标志); xferPaint.setColor(颜色:黑色); 画布绘制路径(clipPath,xferPaint); xfirpaint.setXfermode(新的PorterDuffXfermode(PorterDuff.Mode.DST_-IN)); canvas.drawBitmap(alteredBitmap,0,0,xferPaint); } 私有void doCrop(){ 试一试{ sourceBitmap=MediaStore.Images.Media.getBitmap(this.getContentResolver(),mImageCaptureUri); Options bmpFactoryOptions=新的BitmapFactory.Options(); bmpFactoryOptions.inJustDecodeBounds=true; bmp=BitmapFactory.decodeStream(getContentResolver().openInputStream( mImageCaptureUri),null,bmpFactoryOptions); bmpFactoryOptions.inJustDecodeBounds=false; bmp=BitmapFactory.decodeStream(getContentResolver().openInputStream( mImageCaptureUri),null,bmpFactoryOptions); alteredBitmap=Bitmap.createBitmap(bmp.getWidth(),bmp .getHeight(),bmp.getConfig(); 画布=新画布(alteredBitmap); 油漆=新油漆(); 油漆。设置颜色(颜色。绿色); 油漆。设置行程宽度(5); 矩阵=新矩阵(); 画布.绘图位图(bmp、矩阵、绘画); //在图像视图中加载图像位图 drawImageView.setImageBitmap(alteredBitmap); //设置触摸式侦听器 drawImageView.setOnTouchListener(此); }catch(filenotfounde异常){ e、 printStackTrace(); }捕获(IOE异常){ e、 printStackTrace(); } } },android,crop,ontouch,Android,Crop,Ontouch,这是裁剪图像,但当我绘制裁剪形状的形状时,从远离我触摸的点开始绘制形状,并从我触摸的视图向上绘制形状,请任何人可以建议我在这里做什么错误 firs是原始图像,当我在键盘上画一个非常大的圆圈时,它会在离键盘非常远的地方出现,第二个图像是非常小的圆圈 我在键盘上画了一个圆圈,但它是从上面剪下来的,而且圆圈的大小非常小 我已经解决并完全设置了图像的裁剪和保存,并对自己在屏幕上选择图像和拟合图像进行了一些修改,我的代码如下所示 public class SomeView extends View im

这是裁剪图像,但当我绘制裁剪形状的形状时,从远离我触摸的点开始绘制形状,并从我触摸的视图向上绘制形状,请任何人可以建议我在这里做什么错误

firs是原始图像,当我在键盘上画一个非常大的圆圈时,它会在离键盘非常远的地方出现,第二个图像是非常小的圆圈 我在键盘上画了一个圆圈,但它是从上面剪下来的,而且圆圈的大小非常小


我已经解决并完全设置了图像的裁剪和保存,并对自己在屏幕上选择图像和拟合图像进行了一些修改,我的代码如下所示

public class SomeView extends View implements OnTouchListener {
private Paint paint;
public static List<Point> points;
int DIST = 2;
boolean flgPathDraw = true;
public Uri orignalUri;
float origianlheight=0.0f,originalwidth=0.0f,heightratio=0.0f,widthratio=0.0f;
public static int REQUEST_CODE=2;
Point mfirstpoint = null;
boolean bfirstpoint = false;

Point mlastpoint = null;
boolean cropflag=false;

Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.bg_crop);
Context mContext;


public void setBitmap(Bitmap bmp,Uri uri){
    orignalUri=uri;
    points = new ArrayList<Point>();
    setFocusable(true);
    setFocusableInTouchMode(true);

    DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();
    int width = metrics.widthPixels;
    int height = metrics.heightPixels;
    origianlheight=bmp.getHeight();
    originalwidth=bmp.getWidth();
    //if(origianlheight>originalwidth){
        heightratio=height/origianlheight;
  //  }else{
        widthratio=width/originalwidth;
  //  }
    bitmap=bmp;
    bitmap=Bitmap.createScaledBitmap(bmp, (int)(bmp.getWidth()*widthratio),(int)(bmp.getHeight()*heightratio), true);

 // bitmap=bmp;
  paint = new Paint(Paint.ANTI_ALIAS_FLAG);
  paint.setStyle(Paint.Style.STROKE);
  paint.setPathEffect(new DashPathEffect(new float[] { 10, 20 }, 0));
  paint.setStrokeWidth(5);
  paint.setColor(Color.WHITE);

  flgPathDraw = true;
  this.setOnTouchListener(this);
  points = new ArrayList<Point>();
  bfirstpoint = false;
  cropflag=false;

}
public void clear(){
     setFocusable(true);
     setFocusableInTouchMode(true);
     paint = new Paint(Paint.ANTI_ALIAS_FLAG);
     paint.setStyle(Paint.Style.STROKE);
     paint.setPathEffect(new DashPathEffect(new float[] { 10, 20 }, 0));
     paint.setStrokeWidth(5);
     paint.setColor(Color.WHITE);
     this.setOnTouchListener(this);
     points = new ArrayList<Point>();

     bfirstpoint = false;
     flgPathDraw = true;
     cropflag=false;
     invalidate();
}

public SomeView(Context c) {
    super(c);

    mContext = c;
    setFocusable(true);
    setFocusableInTouchMode(true);

    DisplayMetrics metrics = c.getResources().getDisplayMetrics();
    int width = metrics.widthPixels;
    int height = metrics.heightPixels;
    origianlheight=bitmap.getHeight();
    originalwidth=bitmap.getWidth();
    //if(origianlheight>originalwidth){
        heightratio=height/origianlheight;
  //  }else{
        widthratio=width/originalwidth;
  //  }
    bitmap=Bitmap.createScaledBitmap(bitmap, (int)(originalwidth*widthratio),(int)(origianlheight*heightratio), true);



    paint = new Paint(Paint.ANTI_ALIAS_FLAG);

    paint.setStyle(Paint.Style.STROKE);
    paint.setPathEffect(new DashPathEffect(new float[] { 10, 20 }, 0));
    paint.setStrokeWidth(5);
    paint.setColor(Color.WHITE);

    this.setOnTouchListener(this);
    points = new ArrayList<Point>();

    bfirstpoint = false;
    flgPathDraw = true;
    cropflag=false;
}

public SomeView(Context context, AttributeSet attrs) {
    super(context, attrs);
    mContext = context;
    setFocusable(true);
    setFocusableInTouchMode(true);

    DisplayMetrics metrics = context.getResources().getDisplayMetrics();
    int width = metrics.widthPixels;
    int height = metrics.heightPixels;
    origianlheight=bitmap.getHeight();
    originalwidth=bitmap.getWidth();
    //if(origianlheight>originalwidth){
        heightratio=height/origianlheight;
  //  }else{
        widthratio=width/originalwidth;
  //  }
    bitmap=Bitmap.createScaledBitmap(bitmap, (int)(originalwidth*widthratio),(int)(origianlheight*heightratio), true);

    paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setStyle(Paint.Style.STROKE);
    paint.setPathEffect(new DashPathEffect(new float[] { 10, 20 }, 0));
    paint.setStrokeWidth(5);
    paint.setColor(Color.WHITE);
    this.setOnTouchListener(this);
    points = new ArrayList<Point>();
    bfirstpoint = false;
    flgPathDraw = true;
    cropflag=false;


}

public void onDraw(Canvas canvas) {
    canvas.drawBitmap(bitmap, 0, 0,paint);

    Path path = new Path();
    boolean first = true;

    for (int i = 0; i < points.size(); i += 2) {
        Point point = points.get(i);
        if (first) {
            first = false;
            path.moveTo(point.x, point.y);
        } else if (i < points.size() - 1) {
            Point next = points.get(i + 1);
            path.quadTo(point.x, point.y, next.x, next.y);
        } else {
            mlastpoint = points.get(i);
            path.lineTo(point.x, point.y);
        }
    }
    canvas.drawPath(path, paint);
}
@Override
public boolean onTouch(View view, MotionEvent event) {
    // if(event.getAction() != MotionEvent.ACTION_DOWN)
    // return super.onTouchEvent(event);

    Point point = new Point();
    point.x = (int) event.getX();
    point.y = (int) event.getY();

    if (flgPathDraw) {

        if (bfirstpoint) {

            if (comparepoint(mfirstpoint, point)) {
                // points.add(point);
                points.add(mfirstpoint);
                    flgPathDraw = false;
                                 //  showcropdialog();
                    cropflag=true;
            } else {
                points.add(point);
            }
        } else {
            points.add(point);
        }

        if (!(bfirstpoint)) {

            mfirstpoint = point;
            bfirstpoint = true;
        }
    }

    invalidate();
    Log.e("Hi  ==>", "Size: " + point.x + " " + point.y);

    if (event.getAction() == MotionEvent.ACTION_UP) {
        Log.d("Action up*******~~~~~~~>>>>", "called");
        mlastpoint = point;
        if (flgPathDraw) {
            if (points.size() > 12) {
                if (!comparepoint(mfirstpoint, mlastpoint)) {
                    flgPathDraw = false;
                    points.add(mfirstpoint);
                   // showcropdialog();
                    cropflag=true;

                }
            }
        }
    }

    return true;
}

private boolean comparepoint(Point first, Point current) {
    int left_range_x = (int) (current.x - 3);
    int left_range_y = (int) (current.y - 3);

    int right_range_x = (int) (current.x + 3);
    int right_range_y = (int) (current.y + 3);

    if ((left_range_x < first.x && first.x < right_range_x)
            && (left_range_y < first.y && first.y < right_range_y)) {
        if (points.size() < 10) {
            return false;
        } else {
            return true;
        }
    } else {
        return false;
    }

}

public void fillinPartofPath() {
    Point point = new Point();
    point.x = points.get(0).x;
    point.y = points.get(0).y;

    points.add(point);
    invalidate();
}

public void resetView() {
    points.clear();
    paint.setColor(Color.WHITE);
    paint.setStyle(Style.STROKE);
    flgPathDraw = true;
    invalidate();
}

 /*  private void showcropdialog() {
    DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Intent intent;
            switch (which) {
            case DialogInterface.BUTTON_POSITIVE:
                // Yes button clicked
                // bfirstpoint = false;

                intent = new Intent(mContext, CropActivity.class);
                intent.putExtra("crop", true);
                intent.putExtra("heightratio", heightratio);
                intent.putExtra("widthratio", widthratio);
                intent.putExtra("URI", orignalUri.toString());
                mContext.startActivity(intent);
                break;

            case DialogInterface.BUTTON_NEGATIVE:
                // No button clicked

                intent = new Intent(mContext, CropActivity.class);
                intent.putExtra("crop", false);
                intent.putExtra("heightratio", heightratio);
                intent.putExtra("widthratio", widthratio);
                intent.putExtra("URI", orignalUri.toString());
                mContext.startActivity(intent);

                bfirstpoint = false;
                // resetView();

                break;
            }
        }
    };

    AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
    builder.setMessage("Do you Want to save Crop or Non-crop image?")
            .setPositiveButton("Crop", dialogClickListener)
            .setNegativeButton("Non-crop", dialogClickListener).show()
            .setCancelable(false);
}*/

public void Crop(){
    if(cropflag){
        Intent intent;
        intent = new Intent(mContext, CropActivity.class);
        intent.putExtra("crop", true);
        intent.putExtra("heightratio", heightratio);
        intent.putExtra("widthratio", widthratio);
        intent.putExtra("URI", orignalUri.toString());
        ((Activity)mContext).startActivityForResult(intent,REQUEST_CODE);
    }
}
此贴纸创建者活动允许用户清除裁剪并从媒体中选择图像,或捕获图像,然后再裁剪图像

public class CropActivity extends Activity {
ImageView compositeImageView;
boolean crop;

Bitmap resultingImage;
Uri imageUri;
Button btnDone,btnCancel;
float heightratio=0.0f,widthratio=0.0f;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_crop);

    btnDone=(Button)findViewById(R.id.btnDone);
    btnCancel=(Button)findViewById(R.id.btnCancel);
    btnCancel.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {               
            Intent intent = new Intent(CropActivity.this, StickersCreator.class);
            setResult(RESULT_CANCELED);
            finish();
        }
    });
    btnDone.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
             int widthOfscreen = 0;
            FileOutputStream out = null;
            try {

                Long tsLong = System.currentTimeMillis()/1000;
                String ts = tsLong.toString();

                File Filename=new File(getFilesDir(),ts+".png");
                //File Filename1=new File(getFilesDir(),"EPICBATTLE"+((Long)(System.currentTimeMillis()/1000)).toString()+"_ico.png");
                out = new FileOutputStream(Filename);
                resultingImage.compress(Bitmap.CompressFormat.PNG, 100, out); // bmp is your Bitmap instance
                // PNG is a lossless format, the compression factor (100) is ignored

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (out != null) {
                        out.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            Intent intent = new Intent(CropActivity.this, StickersCreator.class);
            setResult(RESULT_OK);
            finish();

        }
    });

    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        crop = extras.getBoolean("crop");
        heightratio=extras.getFloat("heightratio");
        widthratio=extras.getFloat("widthratio");
        imageUri=Uri.parse(extras.getString("URI"));
       // byte[] byteArray = getIntent().getByteArrayExtra("image");
       // bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
    }
    int widthOfscreen = 0;
    int heightOfScreen = 0;

    DisplayMetrics dm = new DisplayMetrics();
    try {
        getWindowManager().getDefaultDisplay().getMetrics(dm);
    } catch (Exception ex) {
    }
    widthOfscreen = dm.widthPixels;
    heightOfScreen = dm.heightPixels;

    compositeImageView = (ImageView) findViewById(R.id.imageView1);

    Bitmap bitmap2=null;
    try {

        bitmap2 = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    bitmap2=Bitmap.createScaledBitmap(bitmap2, (int)(bitmap2.getWidth()*widthratio),(int)(bitmap2.getHeight()*heightratio), true);

    resultingImage = Bitmap.createBitmap(widthOfscreen,
            heightOfScreen, bitmap2.getConfig());
   // resultingImage=CropBitmapTransparency(resultingImage);
    Canvas canvas = new Canvas(resultingImage);
    Paint paint = new Paint();
    paint.setAntiAlias(true);

    Path path = new Path();
    for (int i = 0; i < SomeView.points.size(); i++) {
        path.lineTo(SomeView.points.get(i).x, SomeView.points.get(i).y);
    }
    canvas.drawPath(path, paint);
    if (crop) {
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));

    } else {
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_OUT));
    }
    canvas.drawBitmap(bitmap2, 0, 0, paint);
    resultingImage=CropBitmapTransparency(resultingImage);
    compositeImageView.setImageBitmap(resultingImage);//setImageBitmap(resultingImage);
}

Bitmap CropBitmapTransparency(Bitmap sourceBitmap){
    int minX = sourceBitmap.getWidth();
    int minY = sourceBitmap.getHeight();
    int maxX = -1;
    int maxY = -1;
    for(int y = 0; y < sourceBitmap.getHeight(); y++)
    {
        for(int x = 0; x < sourceBitmap.getWidth(); x++)
        {
            int alpha = (sourceBitmap.getPixel(x, y) >> 24) & 255;
            if(alpha > 0)   // pixel is not 100% transparent
            {
                if(x < minX)
                    minX = x;
                if(x > maxX)
                    maxX = x;
                if(y < minY)
                    minY = y;
                if(y > maxY)
                    maxY = y;
            }
        }
    }
    if((maxX < minX) || (maxY < minY))
        return null; // Bitmap is entirely transparent

    // crop bitmap to non-transparent area and return:
    return Bitmap.createBitmap(sourceBitmap, minX, minY, (maxX - minX) + 1, (maxY - minY) + 1);
}
公共类CropActivity扩展活动{
ImageView compositeImageView;
布尔裁剪;
位图生成图像;
Uri-imageUri;
按钮btnDone,btnCancel;
浮动高度比=0.0f,宽度比=0.0f;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u裁剪);
btnDone=(按钮)findViewById(R.id.btnDone);
btnCancel=(按钮)findViewById(R.id.btnCancel);
btnCancel.setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
意向意向=新意向(CropActivity.this、StickersCreator.class);
setResult(结果被取消);
完成();
}
});
setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
屏幕的int宽度=0;
FileOutputStream out=null;
试一试{
Long tsLong=System.currentTimeMillis()/1000;
字符串ts=tsLong.toString();
文件名=新文件(getFilesDir(),ts+“.png”);
//File Filename1=新文件(getFilesDir(),“EPICBATTLE”+((Long)(System.currentTimeMillis()/1000)).toString()+“_ico.png”);
out=新文件输出流(文件名);
resultingImage.compress(Bitmap.CompressFormat.PNG,100,out);//bmp是位图实例
//PNG是一种无损格式,压缩因子(100)被忽略
}捕获(例外e){
e、 printStackTrace();
}最后{
试一试{
if(out!=null){
out.close();
}
}捕获(IOE异常){
e、 printStackTrace();
}
}
意向意向=新意向(CropActivity.this、StickersCreator.class);
设置结果(结果\正常);
完成();
}
});
Bundle extras=getIntent().getExtras();
如果(附加值!=null){
裁剪=额外的getBoolean(“裁剪”);
heightratio=额外的getFloat(“heightratio”);
widthratio=extras.getFloat(“widthratio”);
imageUri=Uri.parse(extras.getString(“Uri”);
//字节[]byteArray=getIntent().getByteArrayExtra(“图像”);
//bmp=BitmapFactory.decodeByteArray(byteArray,0,byteArray.length);
}
屏幕的int宽度=0;
屏幕的内部高度=0;
DisplayMetrics dm=新的DisplayMetrics();
试一试{
getWindowManager().getDefaultDisplay().getMetrics(dm);
}捕获(例外情况除外){
}
屏幕宽度=dm.widthP
public class StickersCreator 
extends Activity{
ImageButton btnCapture,btnChooseFromlib,btnClear;
Button btnCrop;
SomeView someView;
private static final String RESULT_BUNDLE_EXTAS = "RESULT_BUNDLE_EXTAS";
private static final String RESULT_BUNDLE_DATA = "RESULT_BUNDLE_DATA";
private static final String RESULT_BUNDLE_REQUEST_CODE = "RESULT_BUNDLE_REQUEST_CODE";
private static final String RESULT_BUNDLE_RESULT_CODE = "RESULT_BUNDLE_RESULT_CODE";

private static final int REQUEST_PICK_IMAGE = 0;
private static final int REQUEST_CAPTURE_IMAGE = 1;
private static final int REQUEST_CROP=2;
private Uri captureImageUri;
private Bundle resultBundle;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_stickers_creator);
    btnCrop=(Button)findViewById(R.id.btnCrop);
    btnCapture=(ImageButton)findViewById(R.id.BtnCapture);
    btnClear=(ImageButton)findViewById(R.id.btnClear);
    btnChooseFromlib=(ImageButton)findViewById(R.id.btnChoosefromLib);
    someView=(SomeView)findViewById(R.id.someView);
    btnCrop.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            someView.Crop();
        }
    });
    btnClear.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            someView.clear();

        }
    });
    btnCapture.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {


            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

            captureImageUri = null;
            try {
                String storageState = Environment.getExternalStorageState();
                if (TextUtils.equals(storageState, Environment.MEDIA_MOUNTED)) {
                    String path = Environment.getExternalStorageDirectory()
                            .getAbsolutePath()
                            + File.separatorChar
                            + "Android/data/"
                            + getApplicationContext().getPackageName()
                            + "/files/";
                    File directory = new File(path);
                    directory.mkdirs();
                    File file = new File(directory,
                            Integer.toHexString((int) System
                                    .currentTimeMillis()) + ".jpg");
                    file.createNewFile();

                    captureImageUri = Uri.fromFile(file);
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, captureImageUri);
                }

            } catch (IOException e) {
                if (BuildConfig.DEBUG) {
                    Log.w("FrameEditor",
                            "Can not create temp file for image capture "
                                    + e.getMessage());
                }
            }
            startActivityForResult(intent, REQUEST_CAPTURE_IMAGE);


        }
    });

    btnChooseFromlib.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Intent.ACTION_PICK,
                    MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(intent, REQUEST_PICK_IMAGE);

        }
    });

}

@Override
protected void onActivityResult(int requestCode, int resultCode,Intent intent) {
    resultBundle = new Bundle();
    if (intent != null) {
        Bundle extras = intent.getExtras();
        resultBundle.putBundle(RESULT_BUNDLE_EXTAS, extras);
        Uri uriData = intent.getData();
        if (uriData != null) {
            resultBundle.putParcelable(RESULT_BUNDLE_DATA, uriData);
        }
    }

    resultBundle.putInt(RESULT_BUNDLE_REQUEST_CODE, requestCode);
    resultBundle.putInt(RESULT_BUNDLE_RESULT_CODE, resultCode);

    super.onActivityResult(requestCode, resultCode, intent);

    handleActivityResult();
}

private void handleActivityResult() {
    if (resultBundle == null) {
        return;
    }
    int requestCode = resultBundle.getInt(RESULT_BUNDLE_REQUEST_CODE);
    int resultCode = resultBundle.getInt(RESULT_BUNDLE_RESULT_CODE);
    Uri dataUri = resultBundle.getParcelable(RESULT_BUNDLE_DATA);
    switch (requestCode) {
    case REQUEST_PICK_IMAGE: {
        if (resultCode == RESULT_OK) {
            if (dataUri != null) {

                String[] filePathColumn = { MediaStore.Images.Media.DATA };

                Cursor cursor = getContentResolver().query(dataUri,
                        filePathColumn, null, null, null);
                cursor.moveToFirst();

                int columnIndex = cursor
                        .getColumnIndex(MediaStore.Images.Media.DATA);
                String filePath = cursor.getString(columnIndex);
                cursor.close();


                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inPreferredConfig = Bitmap.Config.ARGB_8888;
                Bitmap bitmap1 = BitmapFactory.decodeFile(filePath, options);
                Uri uri=Uri.fromFile(new File(filePath));
                someView.setBitmap(bitmap1,uri);
                // Intend for EditorActivity

            }
        }
        break;
    }
    case REQUEST_CAPTURE_IMAGE: {
        if (resultCode == RESULT_OK) {
            if (captureImageUri != null) {
                // Intend for EditorActivity

                try {
                    Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), captureImageUri);
                    someView.setBitmap(bitmap,captureImageUri);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }


            }
        }
        break;

    }
    case REQUEST_CROP:{
        }
    }
    }
    resultBundle = null;

}

}
public class CropActivity extends Activity {
ImageView compositeImageView;
boolean crop;

Bitmap resultingImage;
Uri imageUri;
Button btnDone,btnCancel;
float heightratio=0.0f,widthratio=0.0f;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_crop);

    btnDone=(Button)findViewById(R.id.btnDone);
    btnCancel=(Button)findViewById(R.id.btnCancel);
    btnCancel.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {               
            Intent intent = new Intent(CropActivity.this, StickersCreator.class);
            setResult(RESULT_CANCELED);
            finish();
        }
    });
    btnDone.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
             int widthOfscreen = 0;
            FileOutputStream out = null;
            try {

                Long tsLong = System.currentTimeMillis()/1000;
                String ts = tsLong.toString();

                File Filename=new File(getFilesDir(),ts+".png");
                //File Filename1=new File(getFilesDir(),"EPICBATTLE"+((Long)(System.currentTimeMillis()/1000)).toString()+"_ico.png");
                out = new FileOutputStream(Filename);
                resultingImage.compress(Bitmap.CompressFormat.PNG, 100, out); // bmp is your Bitmap instance
                // PNG is a lossless format, the compression factor (100) is ignored

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (out != null) {
                        out.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            Intent intent = new Intent(CropActivity.this, StickersCreator.class);
            setResult(RESULT_OK);
            finish();

        }
    });

    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        crop = extras.getBoolean("crop");
        heightratio=extras.getFloat("heightratio");
        widthratio=extras.getFloat("widthratio");
        imageUri=Uri.parse(extras.getString("URI"));
       // byte[] byteArray = getIntent().getByteArrayExtra("image");
       // bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
    }
    int widthOfscreen = 0;
    int heightOfScreen = 0;

    DisplayMetrics dm = new DisplayMetrics();
    try {
        getWindowManager().getDefaultDisplay().getMetrics(dm);
    } catch (Exception ex) {
    }
    widthOfscreen = dm.widthPixels;
    heightOfScreen = dm.heightPixels;

    compositeImageView = (ImageView) findViewById(R.id.imageView1);

    Bitmap bitmap2=null;
    try {

        bitmap2 = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    bitmap2=Bitmap.createScaledBitmap(bitmap2, (int)(bitmap2.getWidth()*widthratio),(int)(bitmap2.getHeight()*heightratio), true);

    resultingImage = Bitmap.createBitmap(widthOfscreen,
            heightOfScreen, bitmap2.getConfig());
   // resultingImage=CropBitmapTransparency(resultingImage);
    Canvas canvas = new Canvas(resultingImage);
    Paint paint = new Paint();
    paint.setAntiAlias(true);

    Path path = new Path();
    for (int i = 0; i < SomeView.points.size(); i++) {
        path.lineTo(SomeView.points.get(i).x, SomeView.points.get(i).y);
    }
    canvas.drawPath(path, paint);
    if (crop) {
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));

    } else {
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_OUT));
    }
    canvas.drawBitmap(bitmap2, 0, 0, paint);
    resultingImage=CropBitmapTransparency(resultingImage);
    compositeImageView.setImageBitmap(resultingImage);//setImageBitmap(resultingImage);
}

Bitmap CropBitmapTransparency(Bitmap sourceBitmap){
    int minX = sourceBitmap.getWidth();
    int minY = sourceBitmap.getHeight();
    int maxX = -1;
    int maxY = -1;
    for(int y = 0; y < sourceBitmap.getHeight(); y++)
    {
        for(int x = 0; x < sourceBitmap.getWidth(); x++)
        {
            int alpha = (sourceBitmap.getPixel(x, y) >> 24) & 255;
            if(alpha > 0)   // pixel is not 100% transparent
            {
                if(x < minX)
                    minX = x;
                if(x > maxX)
                    maxX = x;
                if(y < minY)
                    minY = y;
                if(y > maxY)
                    maxY = y;
            }
        }
    }
    if((maxX < minX) || (maxY < minY))
        return null; // Bitmap is entirely transparent

    // crop bitmap to non-transparent area and return:
    return Bitmap.createBitmap(sourceBitmap, minX, minY, (maxX - minX) + 1, (maxY - minY) + 1);
}