Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

I've a bitmap image which contain both transparent area and non-transparent area. I've to find out non-transparent co-ordinates and change its perspective in android.

image description I've to find this angle of the image and change its perspective as per its angle.

I use the following code to achieve that

Bitmap sourceBitmap = BitmapFactory.decodeResource(res, R.drawable.layer1, options); Bitmap wall = BitmapFactory.decodeResource(res, R.drawable.wallpaper2, options); Bitmap descBitmap=Bitmap.createBitmap(sourceBitmap.getWidth(),sourceBitmap.getHeight(), Bitmap.Config.ARGB_8888); // Utils.matToBitmap(imgMat,descBitmap); Bitmap temp=CropBitmapTransparency(sourceBitmap); Bitmap resultBitmap=Bitmap.createBitmap(sourceBitmap.getWidth(),sourceBitmap.getHeight(),Bitmap.Config.ARGB_8888); Mat inputMat = new Mat(); Mat outputMat = new Mat(); Mat outputMat1 = new Mat(); // descBitmap=sourceBitmap; Utils.bitmapToMat(sourceBitmap, inputMat); System.out.println("INput matrix = "+inputMat.toString()); List<point> src_pnt = new ArrayList<point>(); Point p0 = new Point(startWidth,startHeight); src_pnt.add(p0); Point p1 = new Point(endWidth, startHeight); src_pnt.add(p1); Point p2 = new Point(startWidth, endHeight); src_pnt.add(p2); Point p3 = new Point(endWidth, endHeight); src_pnt.add(p3); Mat startM = Converters.vector_Point2f_to_Mat(src_pnt);

        List<Point> dst_pnt = new ArrayList<Point>();
        Point p4 = new Point(0.0, 0.0);
        dst_pnt.add(p4);
        Point p5 = new Point(0.0, sourceBitmap.getHeight());
        dst_pnt.add(p5);
        Point p6 = new Point(sourceBitmap.getWidth(), sourceBitmap.getHeight());
        dst_pnt.add(p6);
        Point p7 = new Point(sourceBitmap.getWidth(), 0);
        dst_pnt.add(p7);
        Mat endM = Converters.vector_Point2f_to_Mat(dst_pnt);
        Mat perspectiveTransform = Imgproc.getPerspectiveTransform(startM, endM);
        Size size = new Size(sourceBitmap.getWidth(), sourceBitmap.getHeight());
        Scalar scalar = new Scalar(50.0);
        Imgproc.warpPerspective(inputMat, outputMat, perspectiveTransform, size, Imgproc.INTER_LINEAR + Imgproc.CV_WARP_FILL_OUTLIERS,Imgproc.COLOR_BayerBG2BGR , scalar);


     Utils.matToBitmap(inputMat, descBitmap);

        image3.setImageBitmap(descBitmap);

PLease help me as soon as possible.

Thanks in advance

click to hide/show revision 2
No.2 Revision

updated 2017-06-19 02:27:54 -0600

berak gravatar image

I've a bitmap image which contain both transparent area and non-transparent area. I've to find out non-transparent co-ordinates and change its perspective in android.

image description I've to find this angle of the image and change its perspective as per its angle.

I use the following code to achieve that

 Bitmap sourceBitmap = BitmapFactory.decodeResource(res, R.drawable.layer1, options);
Bitmap wall = BitmapFactory.decodeResource(res, R.drawable.wallpaper2, options);
Bitmap descBitmap=Bitmap.createBitmap(sourceBitmap.getWidth(),sourceBitmap.getHeight(), Bitmap.Config.ARGB_8888);
// Utils.matToBitmap(imgMat,descBitmap);
Bitmap temp=CropBitmapTransparency(sourceBitmap);
Bitmap resultBitmap=Bitmap.createBitmap(sourceBitmap.getWidth(),sourceBitmap.getHeight(),Bitmap.Config.ARGB_8888);
Mat inputMat = new Mat();
Mat outputMat = new Mat();
Mat outputMat1 = new Mat();
// descBitmap=sourceBitmap;
Utils.bitmapToMat(sourceBitmap, inputMat);
System.out.println("INput matrix = "+inputMat.toString());
List<point> List<Point> src_pnt = new ArrayList<point>();
ArrayList<Point>();
 Point p0 = new Point(startWidth,startHeight);
src_pnt.add(p0);
Point p1 = new Point(endWidth, startHeight);
src_pnt.add(p1);
Point p2 = new Point(startWidth, endHeight);
src_pnt.add(p2);
Point p3 = new Point(endWidth, endHeight);
src_pnt.add(p3);
Mat startM = Converters.vector_Point2f_to_Mat(src_pnt);

Converters.vector_Point2f_to_Mat(src_pnt);
 List<Point> dst_pnt = new ArrayList<Point>();
 Point p4 = new Point(0.0, 0.0);
 dst_pnt.add(p4);
  Point p5 = new Point(0.0, sourceBitmap.getHeight());
 dst_pnt.add(p5);
  Point p6 = new Point(sourceBitmap.getWidth(), sourceBitmap.getHeight());
 dst_pnt.add(p6);
  Point p7 = new Point(sourceBitmap.getWidth(), 0);
 dst_pnt.add(p7);
  Mat endM = Converters.vector_Point2f_to_Mat(dst_pnt);
 Mat perspectiveTransform = Imgproc.getPerspectiveTransform(startM, endM);
 Size size = new Size(sourceBitmap.getWidth(), sourceBitmap.getHeight());
 Scalar scalar = new Scalar(50.0);
  Imgproc.warpPerspective(inputMat, outputMat, perspectiveTransform, size, Imgproc.INTER_LINEAR + Imgproc.CV_WARP_FILL_OUTLIERS,Imgproc.COLOR_BayerBG2BGR , scalar);
 Utils.matToBitmap(inputMat, descBitmap);
  image3.setImageBitmap(descBitmap);

PLease help me as soon as possible.

Thanks in advance

I've a bitmap image which contain both transparent area and non-transparent area. I've to find out non-transparent co-ordinates and change its perspective in android.

image description I've to find this angle of the image and change its perspective as per its angle.

I use find non-transparent coordinate pixels of the following code to achieve thatabove image using following

  Bitmap sourceBitmap = BitmapFactory.decodeResource(res, R.drawable.layer1, options);
         CropBitmapTransparency(Bitmap sourceBitmap)
{
    sourceBitmap.setHasAlpha(true);
    startWidth = sourceBitmap.getWidth();//  int minX
    startHeight= sourceBitmap.getHeight();//  int minY
    endWidth= -1;//  int maxX
    endHeight= -1;//  int maxY
    for(int y = 0; y < sourceBitmap.getHeight(); y++)
    {
        for(int x = 0; x < sourceBitmap.getWidth(); x++)
        {
            int alpha = ((sourceBitmap.getPixel(x, y) & 0xff000000) >> 24);
          //
            if(alpha != 0)   // pixel is not 100% transparent
            {
               // Log.d("Alpha",alpha+" ");
                if(x < startWidth)
                    startWidth = x;
                if(x > endWidth)
                    endWidth = x;
                if(y < startHeight)
                    startHeight = y;
                if(y > endHeight)
                    endHeight = y;
            }
        }
    }
    if((endWidth < startWidth) || (endHeight < startHeight))
        return null; // Bitmap wall = BitmapFactory.decodeResource(res, R.drawable.wallpaper2, options);
            is entirely transparent
    Log.w("Startwidh = ",startWidth+" ");
    Log.w("StartHeight = ",startHeight+" ");
    Log.w("Endwidh = ",endWidth+" ");
    Log.w("End Height = ",endHeight+" ");

   // angle=getAngle(startWidth,startHeight,endWidth,endHeight);

    // crop bitmap to non-transparent area and return:
    return Bitmap.createBitmap(sourceBitmap, startWidth, startHeight, (endWidth - startWidth) + 1, (endHeight - startHeight) + 1);
}

Based on the non-transparent part of the image i've change the perspective using following code

 public Bitmap descBitmap=Bitmap.createBitmap(sourceBitmap.getWidth(),sourceBitmap.getHeight(), Bitmap.Config.ARGB_8888);
       //  Utils.matToBitmap(imgMat,descBitmap);
perspectiveBitmap(Bitmap sourceBitmap)
   {
    Bitmap temp=CropBitmapTransparency(sourceBitmap);
     Bitmap resultBitmap=Bitmap.createBitmap(sourceBitmap.getWidth(),sourceBitmap.getHeight(),Bitmap.Config.ARGB_8888);
     Mat inputMat = new Mat();
     Mat outputMat = new Mat();
     Mat outputMat1 = new Mat();
           // descBitmap=sourceBitmap;
            Utils.bitmapToMat(sourceBitmap, inputMat);
         System.out.println("INput matrix = "+inputMat.toString());
            List<Point> src_pnt = new ArrayList<Point>();
            Point p0 = new Point(startWidth,startHeight);
            src_pnt.add(p0);
            Point p1 = new Point(endWidth, startHeight);
            src_pnt.add(p1);
            Point p2 = new Point(startWidth, endHeight);
            src_pnt.add(p2);
            Point p3 = new Point(endWidth, endHeight);
            src_pnt.add(p3);
            Mat startM = Converters.vector_Point2f_to_Mat(src_pnt);

            List<Point> dst_pnt = new ArrayList<Point>();
            Point p4 = new Point(0.0, 0.0);
            dst_pnt.add(p4);
            Point p5 = new Point(0.0, sourceBitmap.getHeight());
            dst_pnt.add(p5);
            Point p6 = new Point(sourceBitmap.getWidth(), sourceBitmap.getHeight());
            dst_pnt.add(p6);
            Point p7 = new Point(sourceBitmap.getWidth(), 0);
            dst_pnt.add(p7);
        src_mat=new Mat(4,1,CvType.CV_32FC2);
    Mat endM = Converters.vector_Point2f_to_Mat(dst_pnt);
        dest_mat=new Mat(4,1,CvType.CV_32FC2);
    src_mat.put(0,0,startWidth,startHeight,endWidth,startHeight,startWidth,endHeight,endWidth,endHeight);
    dest_mat.put(0,0,0.0,0.0,endWidth,0.0,0.0,endHeight,endWidth,endHeight);
    Mat perspectiveTransform = Imgproc.getPerspectiveTransform(startM, endM);
        perspectiveTransform=Imgproc.getPerspectiveTransform(src_mat,dest_mat);
    Mat dst=inputMat.clone();
    Size size = new Size(sourceBitmap.getWidth(), sourceBitmap.getHeight());
            Scalar scalar = new Scalar(50.0);
             Imgproc.warpPerspective(inputMat, outputMat, dst, perspectiveTransform, size, Imgproc.INTER_LINEAR + Imgproc.CV_WARP_FILL_OUTLIERS,Imgproc.COLOR_BayerBG2BGR , scalar);


         Utils.matToBitmap(inputMat, descBitmap);

            image3.setImageBitmap(descBitmap);
size,Imgproc.INTER_CUBIC);

    Log.e("1=",""+inputMat.cols()+" "+inputMat.rows());
    Log.e("outmat.."," "+outputMat.cols()+" "+outputMat.rows());

    Utils.matToBitmap(dst, resultBitmap);
    //Utils.matToBitmap(tmp, b);

  return  resultBitmap;

}

But i'm not getting the perspective change

PLease help me as soon as possible.

possible. Thanks in advance