Commit dd241b2e authored by Buwaneka 's avatar Buwaneka

add SeperateNumbers.java

parent 82164848
package com.example.count;
import android.content.ContextWrapper;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import java.util.ArrayList;
import java.util.List;
public class SeperateNumbers {
public Bitmap getResizedBitmap(Bitmap image, int maxSize) {
int width = image.getWidth();
int height = image.getHeight();
float bitmapRatio = (float)width / (float) height;
if (bitmapRatio > 1) {
width = maxSize;
height = (int) (width / bitmapRatio);
} else {
height = maxSize;
width = (int) (height * bitmapRatio);
}
return Bitmap.createScaledBitmap(image, width, height, true);
}
public Bitmap toGrayscale(Bitmap bmpOriginal)
{
int width, height;
height = bmpOriginal.getHeight();
width = bmpOriginal.getWidth();
Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bmpGrayscale);
Paint paint = new Paint();
ColorMatrix cm = new ColorMatrix();
cm.setSaturation(0);
ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
paint.setColorFilter(f);
c.drawBitmap(bmpOriginal, 0, 0, paint);
return bmpGrayscale;
}
public Bitmap BlackAndWite(Bitmap src){
GlobleVariable.bitmapArrayPicture.clear();
int width = src.getWidth();
int height = src.getHeight();
Bitmap grayBit= toGrayscale(src);
Bitmap grayBitTT= toGrayscale(src);
int britColor=100;
int LoopCount=0;
List<Integer> listy = new ArrayList();
List<Integer> listx = new ArrayList();
List<Integer> listyM = new ArrayList();
List<Integer> listxM = new ArrayList();
boolean Loop=true;
boolean oneCheck=true;
boolean done=false;
while (Loop){
if((!listxM.isEmpty() && listxM.size()<26) || (!listyM.isEmpty() && listyM.size()<27) ){
britColor=britColor-5;
oneCheck=false;
}
if(oneCheck){
if((!listxM.isEmpty() && listxM.size()>26) || (!listyM.isEmpty() && listyM.size()>27)){
britColor=britColor+5;
}
}
if(LoopCount>4){
Loop=false;
}
LoopCount++;
listx.clear();
listxM.clear();
listy.clear();
listyM.clear();
int a=0;
for (int y = 0; y < height; ++y) {
Boolean flag= true;
int county=0;
for (int x = 0; x < width; ++x) {
// get pixel color
int gray = Color.red(grayBit.getPixel(x, y));
if (gray <britColor) {
county++;
gray=0;
if (county>25){
flag=false;
}
}
else{
gray=255;
}
grayBitTT.setPixel(x,y, Color.rgb(gray, gray, gray));
}
if(flag){
if(listy.isEmpty()){
listy.add(y);
a=y;
}
else{
if (!(a+1 == y )){
if ((listy.contains(a)) ){
listy.add(y);
a=y;
}
else{
listy.add(a);
listy.add(y);
a=y;
}
}
else{
a=a+1;
}
}
}
}
a=0;
for (int x = 0; x < width; ++x) {
Boolean flag= true;
int countx=0;
for (int y = 0; y < height; ++y) {
// get pixel color
int gray = Color.red(grayBit.getPixel(x, y));
if (gray < britColor) {
countx++;
if (countx>50){
flag=false;
break;
}
}
}
if(flag){
if(listx.isEmpty()){
listx.add(x);
a=x;
}
else{
if (!(a+1 == x )){
if ((listx.contains(a)) ){
listx.add(x);
a=x;
}
else {
listx.add(a);
listx.add(x);
a=x;
}
}
else{
a=a+1;
}
}
}
}
listy.add(height);
listx.add(width);
for (int x = 0; x< listx.size()-1; x=x+2){
listxM.add((int)(listx.get(x)+listx.get(x+1))/2);
}
for (int x = 0; x< listy.size()-1; x=x+2){
listyM.add((int)(listy.get(x)+listy.get(x+1))/2);
}
boolean flagchceck=true;
if(listxM.size()==26 && listyM.size()==27){
int countCheckx=0;
for (int x = 0; x < 26; x++) {
for (int y = 0; y < height; ++y) {
int gray = Color.red(grayBit.getPixel(listxM.get(x), y));
if (gray < britColor) {
countCheckx++;
if (countCheckx>50){
flagchceck=false;
break;
}
}
}
}
int countChecky=0;
for (int y = 0; y < 27; ++y) {
for (int x = 0; x < width; ++x) {
int gray = Color.red(grayBit.getPixel(x, listyM.get(y)));
if (gray < britColor) {
countChecky++;
if (countChecky>25){
flagchceck=false;
break;
}
}
}
}
}
if(listxM.size()==26 && listyM.size()==27 && flagchceck){
for (int x = 0; x < listxM.size(); x++) {
for (int y = 0; y < height; y++) {
src.setPixel(listxM.get(x), y, Color.rgb(0, 0, 0));
}
}
for (int x = 0; x < listyM.size(); x++) {
for (int y = 0; y < width; y++) {
src.setPixel(y,listyM.get(x), Color.rgb(0, 0, 0));
}
}
for(int y=0;y<26;y++){
for(int x=0;x<25;x++){
Bitmap resizedbitmap1=Bitmap.createBitmap(src, listxM.get(x),listyM.get(y),listxM.get(x+1)-listxM.get(x), listyM.get(y+1)-listyM.get(y));
GlobleVariable.bitmapArrayPicture.add(resizedbitmap1);
}
}
Loop=false;
done=true;
}
}
GlobleVariable.done=done;
return src;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment