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.

asked 2017-06-19 02:09:02 -0500

Aadhi gravatar image

updated 2017-06-19 08:00:56 -0500

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

I find non-transparent coordinate pixels of the above image using following

 Bitmap 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 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 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();

    Utils.bitmapToMat(sourceBitmap, inputMat);
    Mat src_mat=new Mat(4,1,CvType.CV_32FC2);
    Mat 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(src_mat,dest_mat);
    Mat dst=inputMat.clone();
    Size size = new Size(sourceBitmap.getWidth(), sourceBitmap.getHeight());

    Imgproc.warpPerspective(inputMat, dst, perspectiveTransform, 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. Thanks in advance

edit retag flag offensive close merge delete

Comments

the angle and the perspective of what , exactly ?

where do startWidth, startHeight, etc. come from ?

berak gravatar imageberak ( 2017-06-19 02:30:50 -0500 )edit

Imgproc.COLOR_BayerBG2BGR is for sure the wrong flag here. (BORDERMODE)

berak gravatar imageberak ( 2017-06-19 02:32:20 -0500 )edit