// CharacterExtractor.java // Copyright (c) 2010 William Whitney // All rights reserved. // This software is released under the BSD license. // Please see the accompanying LICENSE.txt for details. package srtp.ys.ocrtest; import java.util.logging.Level; import java.util.logging.Logger; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.ColorMatrix; import android.graphics.ColorMatrixColorFilter; import android.graphics.Paint; import android.graphics.Matrix; import android.graphics.Bitmap.Config; import android.graphics.drawable.BitmapDrawable; import android.util.Log; import android.widget.ImageView; import android.app.*; import android.app.Activity; import android.os.Bundle; import android.view.View; /** * Saves all the characters in an image to an output directory individually. * @author William Whitney */ public class CharacterExtractor extends DocumentScannerListenerAdaptor { private DocumentScanner documentScanner = new DocumentScanner(); private int std_width; private int std_height; //////////////////////////////////////////////////////////////////// public void slice( int std_width, int std_height) { try { this.std_width = std_width; this.std_height = std_height; Bitmap img = BitmapFactory.decodeFile("/sdcard/test.bmp"); PixelImage pixelImage = new PixelImage(img); pixelImage.toGrayScale(true); //OK pixelImage.filter(); // OK documentScanner.scan(pixelImage, this, 0, 0, pixelImage.width, pixelImage.height); } catch (Exception ex) { Logger.getLogger(CharacterExtractor.class.getName()).log(Level.SEVERE, null, ex); } } //////////////////////////////////////////////////////////////////// public static Bitmap zoomBitmap(Bitmap bitmap, int w, int h) { int width = bitmap.getWidth(); int height = bitmap.getHeight(); Matrix matrix = new Matrix(); float scaleWidth ; float scaleHeight; //float scaleWidth = ((float) w / width); if(height>width) { scaleWidth = ((float) h / height); scaleHeight = ((float) h / height); } else { scaleWidth = ((float) w / width); scaleHeight = ((float) w / width); } matrix.postScale(scaleWidth, scaleHeight); Bitmap newBmp = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true); //加上30*30白色背景 Bitmap mbmpTest = Bitmap.createBitmap(30,30, Config.RGB_565); Canvas canvasTemp = new Canvas(mbmpTest);//给Canvas指定一个bitmap,之后就不是画到屏幕而是画到这个bitmap上 canvasTemp.drawColor(Color.WHITE); Paint mPaint = new Paint(); mPaint.setColor(Color.WHITE); canvasTemp.drawBitmap(newBmp,(((float)(30-newBmp.getWidth()))/2),(((float)(30-newBmp.getHeight()))/2),mPaint);//画到正中间 return mbmpTest; } public static Bitmap rgbtoGrayscale(Bitmap bmpOriginal) { int width, height; height = bmpOriginal.getHeight(); width = bmpOriginal.getWidth(); Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565); 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 void processChar(int x1, int y1, int x2, int y2, int rowY1, int rowY2) { try { int areaW = x2 - x1; int areaH = y2 - y1; //Extract the character Bitmap fatherimage = BitmapFactory.decodeFile("/sdcard/test.bmp"); // Drawable characterImage=new BitmapDrawable(BitmapFactory.decodeFile("/sdcard/test.jpg")); Bitmap bitmapcharacterImage = Bitmap.createBitmap(fatherimage,x1, y1, areaW, areaH); Log.v("w","w:"+bitmapcharacterImage.getWidth()+"H:"+bitmapcharacterImage.getHeight()); Bitmap characterImage=zoomBitmap(bitmapcharacterImage,std_width,std_height);//缩放并添上背景 // 显示位图 BitmapDrawable bmpDraw=new BitmapDrawable(rgbtoGrayscale(characterImage)); mainactivity.iv2.setImageDrawable(bmpDraw); // int[] testImgPixels = ImageUtils.getPixels(rgbtoGrayscale(characterImage), characterImage.getWidth( ), characterImage.getHeight( )); // int[][] binerymerticx=ImageUtils.getSymbolMatrix(testImgPixels, 2); int[] binerymerticxforcompare = ImageUtils.getbinarymatrix(rgbtoGrayscale(characterImage)); int countblackpoint=0; for(int ii=0;ii<30;ii++) for(int jj=0;jj<30;jj++) if(binerymerticxforcompare[ii*30+jj]==1) countblackpoint++; if(countblackpoint<10) { System.out.println(); } else { //此处进行比较并输出结果: int i; int j; int maxequal=0; int maxindex=0; int count=0; for(i=0;i<26;i++) { count=0; for(j=0;j<900;j++) { if(model.CharacterModel1[j]==binerymerticxforcompare[j]) count++; } // System.out.print(count); // System.out.print(" "); if(count>maxequal) { maxequal=count; maxindex=i; } } int i2; int j2; int maxequal2=0; int maxindex2=0; int count2=0; for(i2=0;i2<26;i2++) { count2=0; for(j2=0;j2<900;j2++) { if(model2.CharacterModel2[i2][j2]==binerymerticxforcompare[j2]) count2++; } // System.out.print(count); // System.out.print(" "); if(count2>maxequal2) { maxequal2=count2; maxindex2=i2; } } char result; if(maxequal>maxequal2) result=(char)('A'+maxindex); else result=(char)('a'+maxindex2); mainactivity.res+=result; // System.out.print(result); // System.out.println(); } } catch (Exception ex) { Logger.getLogger(CharacterExtractor.class.getName()).log(Level.SEVERE, null, ex); } } private static final Logger LOG = Logger.getLogger(CharacterExtractor.class.getName()); }