2016-03-30 8 views
7

पर कुछ मामलों में एक क्वाड्रैंगल में छवि को विकृत करना विफल रहता है, मैं एक बिटमैप के एक चयनित क्षेत्र (4 कोनों) को एक आयत में बदलने के लिए matrix.setPolyToPoly फ़ंक्शन का उपयोग कर रहा हूं और आमतौर पर यह अद्भुत काम करता है। लेकिन अगले उदाहरण में:एंड्रॉइड

4 corners selection image

polyToPoly समारोह परिप्रेक्ष्य बदलने विफल रहता है:

Bad transformation image

मैं परीक्षण के लिए दो पंक्तियों तैयार की है, लाइनों चिह्नित जहां मैं चार चयनित अंक चाहते हैं ।

मैं क्या गलत कर रहा हूं? धन्यवाद!

संपादित करें: मैंने आपकी सलाह के लिए canvas.drawBitmapMesh का उपयोग कर समस्या हल की है, धन्यवाद pskink !!

इस अंतिम कोड

private float[] generateVertices(int widthBitmap, int heightBitmap) { 
    float[] vertices=new float[(WIDTH_BLOCK+1)*(HEIGHT_BLOCK+1)*2]; 

    float widthBlock = (float)widthBitmap/WIDTH_BLOCK; 
    float heightBlock = (float)heightBitmap/HEIGHT_BLOCK; 

    for(int i=0;i<=HEIGHT_BLOCK;i++) 
     for(int j=0;j<=WIDTH_BLOCK;j++) { 
      vertices[i * ((HEIGHT_BLOCK+1)*2) + (j*2)] = j * widthBlock; 
      vertices[i * ((HEIGHT_BLOCK+1)*2) + (j*2)+1] = i * heightBlock; 
     } 
    return vertices; 
} 

private Bitmap perspectiveTransformation(Bitmap bitmap, ArrayList<Point> bitmapPoints) { 

    Bitmap correctedBitmap; 
    int maxX = (int) Math.max(Math.abs(bitmapPoints.get(0).x - bitmapPoints.get(1).x), Math.abs(bitmapPoints.get(2).x - bitmapPoints.get(3).x)); 
    int maxY = (int) Math.max(Math.abs(bitmapPoints.get(0).y - bitmapPoints.get(3).y), Math.abs(bitmapPoints.get(1).y - bitmapPoints.get(2).y)); 
    Log.d("max", "x=" + maxX + " y=" + maxY); //This is the desired final size 

    Bitmap.Config conf = Bitmap.Config.ARGB_8888; 
    correctedBitmap = Bitmap.createBitmap(maxX,maxY,conf); //the final bitmap 
    float mVertices[] =generateVertices(bitmap.getWidth(),bitmap.getHeight()); 

    Point mLeftTop = bitmapPoints.get(0); 
    Point mRightTop = bitmapPoints.get(1); 
    Point mLeftBot = bitmapPoints.get(3); 
    Point mRightBot = bitmapPoints.get(2); //the points on the image where the user has clicked 

    Canvas canvas = new Canvas(correctedBitmap); 

    Matrix matrix = new Matrix(); 
    matrix.setPolyToPoly(
      new float[]{mLeftTop.x, mLeftTop.y, 
        mRightTop.x, mRightTop.y, 
        mRightBot.x, mRightBot.y, 
        mLeftBot.x, mLeftBot.y //the user's points 
      }, 
      0, 
      new float[]{0, 0, 
        maxX - 1, 0, 
        maxX - 1, maxY - 1, 
        0, maxY - 1    //where I want the user points in the corrected image 
      } 
      , 0, 4); 

    canvas.concat(matrix); 

    Paint paint = new Paint(); 
    paint.setAntiAlias(true);  //testing parameters 
    paint.setFilterBitmap(true); //testing parameters 

    paint.setColor(Color.BLUE); 
    paint.setStyle(Paint.Style.STROKE); 

    canvas.drawBitmapMesh(bitmap, WIDTH_BLOCK , HEIGHT_BLOCK, mVertices,0,null,0, paint); //draw the original bitmap into the corrected bitmap with PolyToPoly transformation matrix 

    canvas.drawLine(mLeftTop.x, mLeftTop.y, mRightBot.x, mRightBot.y, paint); //draw two lines for testing the transformation matrix 
    canvas.drawLine(mLeftBot.x, mLeftBot.y, mRightTop.x, mRightTop.y, paint); 

    //bitmap.recycle(); //just testing 

    return correctedBitmap; 
} 
+1

आप 'setPolyToPoly' के साथ सब कुछ नहीं कर सकते है, कोशिश' Canvas.drawBitmapMesh' बजाय – pskink

+0

धन्यवाद! मैंने अंतिम कोड के साथ प्रश्न अद्यतन किया है। –

+0

@PepSantacruz एक स्पष्ट, अच्छी तरह से व्यक्त प्रश्न के लिए धन्यवाद। एसओ की भावना यह है कि मूल पोस्ट के बाद दूसरों के लिए प्रश्न और उत्तर उपयोगी हो सकते हैं। क्या यह भावना है, क्या आप अपने प्रश्न में संपादन को वापस कर सकते हैं ताकि कोड मूल पोस्ट (आपके द्वारा किए गए उत्तर में काम करने वाले कोड के साथ) हो? साथ ही, क्या आप अपना जवाब स्वीकार कर सकते हैं। इन चीजों को करके, भविष्य के आगंतुक उस समस्या के बीच स्पष्ट अंतर देख पाएंगे जिसमें समस्या थी और समस्या को हल करने वाले कोड। 'स्वीकार करें' एक स्पष्ट संकेत देगा कि इस उत्तर ने समस्या को हल किया है। – Gary99

उत्तर

1
private float[] generateVertices(int widthBitmap, int heightBitmap) { 
    float[] vertices=new float[(WIDTH_BLOCK+1)*(HEIGHT_BLOCK+1)*2]; 

    float widthBlock = (float)widthBitmap/WIDTH_BLOCK; 
    float heightBlock = (float)heightBitmap/HEIGHT_BLOCK; 

    for(int i=0;i<=HEIGHT_BLOCK;i++) 
     for(int j=0;j<=WIDTH_BLOCK;j++) { 
      vertices[i * ((HEIGHT_BLOCK+1)*2) + (j*2)] = j * widthBlock; 
      vertices[i * ((HEIGHT_BLOCK+1)*2) + (j*2)+1] = i * heightBlock; 
     } 
    return vertices; 
} 

private Bitmap perspectiveTransformation(Bitmap bitmap, ArrayList<Point> bitmapPoints) { 

    Bitmap correctedBitmap; 
    int maxX = (int) Math.max(Math.abs(bitmapPoints.get(0).x - bitmapPoints.get(1).x), Math.abs(bitmapPoints.get(2).x - bitmapPoints.get(3).x)); 
    int maxY = (int) Math.max(Math.abs(bitmapPoints.get(0).y - bitmapPoints.get(3).y), Math.abs(bitmapPoints.get(1).y - bitmapPoints.get(2).y)); 
    Log.d("max", "x=" + maxX + " y=" + maxY); //This is the desired final size 

    Bitmap.Config conf = Bitmap.Config.ARGB_8888; 
    correctedBitmap = Bitmap.createBitmap(maxX,maxY,conf); //the final bitmap 
    float mVertices[] =generateVertices(bitmap.getWidth(),bitmap.getHeight()); 

    Point mLeftTop = bitmapPoints.get(0); 
    Point mRightTop = bitmapPoints.get(1); 
    Point mLeftBot = bitmapPoints.get(3); 
    Point mRightBot = bitmapPoints.get(2); //the points on the image where the user has clicked 

    Canvas canvas = new Canvas(correctedBitmap); 

    Matrix matrix = new Matrix(); 
    matrix.setPolyToPoly(
      new float[]{mLeftTop.x, mLeftTop.y, 
        mRightTop.x, mRightTop.y, 
        mRightBot.x, mRightBot.y, 
        mLeftBot.x, mLeftBot.y //the user's points 
      }, 
      0, 
      new float[]{0, 0, 
        maxX - 1, 0, 
        maxX - 1, maxY - 1, 
        0, maxY - 1    //where I want the user points in the corrected image 
      } 
      , 0, 4); 

    canvas.concat(matrix); 

    Paint paint = new Paint(); 
    paint.setAntiAlias(true);  //testing parameters 
    paint.setFilterBitmap(true); //testing parameters 

    paint.setColor(Color.BLUE); 
    paint.setStyle(Paint.Style.STROKE); 

    canvas.drawBitmapMesh(bitmap, WIDTH_BLOCK , HEIGHT_BLOCK, mVertices,0,null,0, paint); //draw the original bitmap into the corrected bitmap with PolyToPoly transformation matrix 

    canvas.drawLine(mLeftTop.x, mLeftTop.y, mRightBot.x, mRightBot.y, paint); //draw two lines for testing the transformation matrix 
    canvas.drawLine(mLeftBot.x, mLeftBot.y, mRightTop.x, mRightTop.y, paint); 

    //bitmap.recycle(); //just testing 

    return correctedBitmap; 
} 
+0

महान उत्तर और प्रश्न के लिए धन्यवाद - धन्यवाद – Fattie

+0

धन्यवाद देने के लिए एक बाउंटी भेजा !! @Fattie –