Last active
August 29, 2015 06:27
-
-
Save zhangyangjing/9d9cc22313959ca55528 to your computer and use it in GitHub Desktop.
extract points position from image
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.zyj.myapplication; | |
import android.content.Context; | |
import android.graphics.Bitmap; | |
import android.graphics.BitmapFactory; | |
import android.graphics.Canvas; | |
import android.graphics.Color; | |
import android.graphics.Paint; | |
import android.graphics.Point; | |
import android.graphics.Rect; | |
import android.util.AttributeSet; | |
import android.view.View; | |
public class MainView extends View { | |
private static final String TAG = MainView.class.getName(); | |
private Bitmap mBmpBall; | |
public MainView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
mBmpBall = BitmapFactory.decodeResource(getResources(), R.drawable.ball); | |
} | |
@Override | |
protected void onDraw(Canvas canvas) { | |
super.onDraw(canvas); | |
canvas.drawBitmap(mBmpBall, 0, 0, null); | |
canvas.drawBitmap(procBall(), 350, 350, null); | |
} | |
private Bitmap procBall() { | |
Bitmap tmpBmp = mBmpBall.copy(Bitmap.Config.ARGB_8888, true); | |
Bitmap bmp = Bitmap.createBitmap(mBmpBall.getWidth(), mBmpBall.getHeight(), Bitmap.Config.ARGB_8888); | |
Canvas canvas = new Canvas(bmp); | |
Paint paint = new Paint(); | |
paint.setColor(Color.RED); | |
paint.setStrokeWidth(4); | |
paint.setAntiAlias(true); | |
for (int y = 0; y < mBmpBall.getHeight(); y++) { | |
for (int x = 0; x < mBmpBall.getWidth(); x++) { | |
if (isWhiteColor(tmpBmp, x, y)) { | |
Point point = getPoint(tmpBmp, x, y); | |
canvas.drawPoint(point.x, point.y, paint); | |
} | |
} | |
} | |
return bmp; | |
} | |
private Point getPoint(Bitmap bmp, int x, int y) { | |
Rect rect = new Rect(x, y, x, y); | |
bmp.setPixel(x, y, Color.BLACK); | |
recursioGetRect(bmp, rect, x, y); | |
return new Point(rect.centerX(), rect.centerY()); | |
} | |
private void recursioGetRect(Bitmap bmp, Rect rect, int x, int y) { | |
_recursionGetRegion(bmp, rect, x - 1, y); // left | |
_recursionGetRegion(bmp, rect, x, y + 1); // top | |
_recursionGetRegion(bmp, rect, x + 1, y); // right | |
_recursionGetRegion(bmp, rect, x, y - 1); // bottom | |
} | |
private void _recursionGetRegion(Bitmap bmp, Rect rect, int x, int y) { | |
if (false == new Rect(0, 0, bmp.getWidth(), bmp.getHeight()).contains(x, y)) | |
return; | |
if (false == isWhiteColor(bmp, x, y)) | |
return; | |
bmp.setPixel(x, y, Color.BLACK); | |
rect.union(x, y); | |
recursioGetRect(bmp, rect, x, y); | |
} | |
private boolean isWhiteColor(Bitmap bmp, int x, int y) { | |
int color = bmp.getPixel(x, y); | |
if (0xff000000 != color) | |
return true; | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment