2012-10-14 4 views
0

J'ai ce codecrash dans Android App

public void DetectFacesInImage(View view) 
    { 
     BitmapFactory.Options bitmapFactoryOptions=new BitmapFactory.Options(); 
     bitmapFactoryOptions.inPreferredConfig= Bitmap.Config.RGB_565; 
     //int resID = getResources().getIdentifier(picturePath , "drawable", getPackageName()); 

     myBitmap=BitmapFactory.decodeFile(picturePath,bitmapFactoryOptions); 
     int width=myBitmap.getWidth(); 
     int height=myBitmap.getHeight(); 
     detectedFaces=new FaceDetector.Face[number_of_faces]; 
     FaceDetector faceDetector=new FaceDetector(width,height,number_of_faces); 
     number_of_faces_detected = faceDetector.findFaces(myBitmap, detectedFaces); 
     Canvas canvas = new Canvas(myBitmap);// line 70 the point where it crashes 
     Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG); 
     //canvas.drawBitmap(overlay, 0, 0, paint); 
     paint.setColor(Color.GREEN); 
     paint.setStyle(Paint.Style.STROKE); 
     paint.setStrokeWidth(3); 
     PointF midPoint=new PointF(); 

     for(int count=0;count<number_of_faces_detected;count++) 
     { 
      Face face=detectedFaces[count]; 

      face.getMidPoint(midPoint); 

      float eyeDistance=face.eyesDistance(); 
      canvas.drawRect(midPoint.x-eyeDistance, midPoint.y-eyeDistance, midPoint.x+eyeDistance, midPoint.y+eyeDistance, paint); 
     } 


    } 

Je veux dessiner le rectangle sur le dessus du bitmap. Le bitmap est reçu par l'intention est est utilisé comme celui-ci

public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_detect_faces); 
     // getActionBar().setDisplayHomeAsUpEnabled(true); 
     Intent intent = getIntent(); 
     ImageView imageView = (ImageView) findViewById(R.id.imgView); 
     picturePath = intent.getStringExtra(GetFaceActivity.PICTURE_PATH); 
     imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath)); 
     //imageView.setOnTouchListener(this); 

    } 

mais le code se bloque avec l'erreur bitmap Immuable passé à constructeur toile. Qu'est-ce que je fais mal ici?

Voici la trace de la pile

10-14 20:01:34.763: E/AndroidRuntime(31137): FATAL EXCEPTION: main 
10-14 20:01:34.763: E/AndroidRuntime(31137): java.lang.IllegalStateException: Could not execute method of the activity 
10-14 20:01:34.763: E/AndroidRuntime(31137): at android.view.View$1.onClick(View.java:3098) 
10-14 20:01:34.763: E/AndroidRuntime(31137): at android.view.View.performClick(View.java:3620) 
10-14 20:01:34.763: E/AndroidRuntime(31137): at android.view.View$PerformClick.run(View.java:14322) 
10-14 20:01:34.763: E/AndroidRuntime(31137): at android.os.Handler.handleCallback(Handler.java:605) 
10-14 20:01:34.763: E/AndroidRuntime(31137): at android.os.Handler.dispatchMessage(Handler.java:92) 
10-14 20:01:34.763: E/AndroidRuntime(31137): at android.os.Looper.loop(Looper.java:137) 
10-14 20:01:34.763: E/AndroidRuntime(31137): at android.app.ActivityThread.main(ActivityThread.java:4507) 
10-14 20:01:34.763: E/AndroidRuntime(31137): at java.lang.reflect.Method.invokeNative(Native Method) 
10-14 20:01:34.763: E/AndroidRuntime(31137): at java.lang.reflect.Method.invoke(Method.java:511) 
10-14 20:01:34.763: E/AndroidRuntime(31137): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:978) 
10-14 20:01:34.763: E/AndroidRuntime(31137): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745) 
10-14 20:01:34.763: E/AndroidRuntime(31137): at dalvik.system.NativeStart.main(Native Method) 
10-14 20:01:34.763: E/AndroidRuntime(31137): Caused by: java.lang.reflect.InvocationTargetException 
10-14 20:01:34.763: E/AndroidRuntime(31137): at java.lang.reflect.Method.invokeNative(Native Method) 
10-14 20:01:34.763: E/AndroidRuntime(31137): at java.lang.reflect.Method.invoke(Method.java:511) 
10-14 20:01:34.763: E/AndroidRuntime(31137): at android.view.View$1.onClick(View.java:3093) 
10-14 20:01:34.763: E/AndroidRuntime(31137): ... 11 more 
10-14 20:01:34.763: E/AndroidRuntime(31137): Caused by: java.lang.IllegalStateException: Immutable bitmap passed to Canvas constructor 
10-14 20:01:34.763: E/AndroidRuntime(31137): at android.graphics.Canvas.<init>(Canvas.java:133) 
10-14 20:01:34.763: E/AndroidRuntime(31137): at com.example.face_tag_upload.DetectFaces.DetectFacesInImage(DetectFaces.java:70) 
10-14 20:01:34.763: E/AndroidRuntime(31137): ... 14 more 
+0

Veuillez publier la journalisation LogCat en affichant la trace de pile du plantage. – Ridcully

+0

pouvez-vous s'il vous plaît mettre en évidence la ligne: DetectFaces.java:70 – Soham

+0

ce que je reçois est le bitmap doit être modifiable ou c'est-à-dire mutable. Mais je ne veux pas en créer une copie. Je veux juste dessiner des ractangles en plus. Y a-t-il une autre méthode? – Ankuj

Répondre

1

Si vous travaillez avec l'API de niveau 11 et vous vous devez spécifier inMutable dans le cadre de votre BitMapFactory.Options, donc;

BitmapFactory.Option bitmapFactoryOptions = new BitmapFactory.Options(); 
bitmapFactoryOptions.inPreferredConfig = Bitmap.Config.RGB_565; 
bitmapFactoryOption.inMutable = true; 

Si vous voulez aller plus tôt que l'API de niveau 11, vous voulez faire un bitmap mutable en utilisant l'extrait suivant;

Bitmap immutableBitmap = BitmapFactory.decodeFile(picturePath, bitmapFactoryOptions); 
int left = 0; 
int top = 0; 
int width = immutableBitmap.getWidth(); 
int height = immutableBitmap.getHeight(); 
boolean hasAlpha = immutableBitmap.hasAlpha(); 
Bitmap mutableBitmap = immutableBitmap.createBitmap(width, height, hasAlpha); 
Canvas c = new Canvas(); 
c.setDevice(mutableBitmap); 
c.drawBitmap(immutableBitmap, left, top, paint); 

La variable de peinture provient de votre code préexistant.

Cette answer écrit la bitmap d'origine sur le disque, puis crée une version mutable de la bitmap, évitant ainsi d'avoir deux bitmaps en mémoire.

+0

Mais ici, vous créez une copie de bitmap pour le niveau d'API inférieur à 11. N'y a-t-il pas d'autre méthode où je n'ai pas besoin de créer une copie? – Ankuj

+0

Malheureusement, je crois que vous devez créer une copie de l'image bitmap. Si vous avez peur d'avoir le même bitmap, le lien que j'ai ajouté à ma réponse évite le besoin de deux bitmaps. –

+0

Thanx marcus qui a aidé mais hélas les rectangles ne sont pas encore visibles sur le bitmap, même si le code détecte 5 visages – Ankuj