2016-07-14 13 views
1

Ich versuche ein Bild zu speichern, wie von Android abgeschnitten und dann in meiner App anzeigen. Ich verwende den unten stehenden Code, aber wenn ich versuche, das Bild in meiner App zu sehen, ist die Bildqualität nicht so gut wie im angehängten Bild. Mache ich etwas falsch? Jede Hilfe wäre großartig.Android beschnitten Bildqualität Problem

printscreeiphone6

Mein Code ist:

dipToPixel = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, getResources().getDisplayMetrics()); 

public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 

if (requestCode == 1 && resultCode == getActivity().RESULT_OK && data != null) { 

    picUri = data.getData(); 

    performCrop(); 

} 


if (requestCode == 111 && resultCode == getActivity().RESULT_OK && data != null) { 

     Bundle extras = data.getExtras(); 

     Bitmap bitmapImage = extras.getParcelable("data"); 

     tweetImage.setImageBitmap(bitmapImage); 

     tweetImage.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { 
      public boolean onPreDraw() { 
       tweetImage.getViewTreeObserver().removeOnPreDrawListener(this); 

       widthPixel = tweetImage.getMeasuredWidth(); 
       heightPixel = tweetImage.getMeasuredHeight(); 

       return true; 

      } 
     }); 

     System.out.println("photo added"); 

     addPhotoVar = 1; 
     addPhotoBtn.setText("remove"); 


} 

callbackManager.onActivityResult(requestCode, resultCode, data); 


} 


private void performCrop() { 

try { 

    //call the standard crop action intent (the user device may not support it) 
    Intent cropIntent = new Intent("com.android.camera.action.CROP"); 
    //indicate image type and Uri 
    cropIntent.setDataAndType(picUri, "image/*"); 
    //set crop properties 
    cropIntent.putExtra("crop", "true"); 
    //indicate aspect of desired crop 
    cropIntent.putExtra("aspectX", 1); 
    cropIntent.putExtra("aspectY", 1); 
    //indicate output X and Y 

    cropIntent.putExtra("outputX", Math.round(screenWidth/dipToPixel)-10); 
    cropIntent.putExtra("outputY", Math.round(screenWidth/dipToPixel)-10); 
    //retrieve data on return 
    cropIntent.putExtra("return-data", true); 
    //start the activity - we handle returning in onActivityResult 
    startActivityForResult(cropIntent, 111); 
} 
// respond to users whose devices do not support the crop action 
catch (ActivityNotFoundException anfe) { 
    // display an error message 
    String errorMessage = "your device doesn't support the crop action!"; 
    Toast toast = Toast.makeText(getActivity(), errorMessage, Toast.LENGTH_SHORT); 
    toast.show(); 
} 
} 

Unterhalb der Code ist, dass ich das Bild verwenden und Datenbank speichern:

   tweetImage.buildDrawingCache(); 
       bm = tweetImage.getDrawingCache(); 

       if (widthPixel < heightPixel) { 

        basePixel = widthPixel; 

       } 

       else { 

        basePixel = heightPixel; 

       } 


       if (basePixel > 768) { 

        widthRatio = (float) 768/basePixel; 
        heightRatio = (float) 768/basePixel; 


       } 

       else { 

        widthRatio = 1; 
        heightRatio = 1; 

       } 


       Bitmap bmResized = Bitmap.createScaledBitmap(bm,(int)(widthPixel*widthRatio), (int)(heightPixel*heightRatio), true); 

       ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
       bmResized.compress(Bitmap.CompressFormat.JPEG, 100, stream); 
       byteArray1 = stream.toByteArray(); 

       image1 = new ParseFile("profilePhoto.jpg", byteArray1, "image/jpg"); 

Antwort

1

Wechsel:

bmResized.compress(Bitmap.CompressFormat.JPEG, 100, stream); 

zu:

bmResized.compress(Bitmap.CompressFormat.PNG, 100, stream); 

Da JPEG Format eine verlustbehaftete Kompression verwendet, sollten Sie PNG verwenden, um die Bitmap zu speichern, wenn Sie nicht Qualitätsverlust wollen.

Auch sollten Sie vermeiden, com.android.camera.action.CROP Absicht zu verwenden, da es nicht auf allen Geräten vorhanden ist, wie here erklärt.

Es gibt einige Alternativen auf dem obigen Link aufgelistet, Sie können einen von ihnen verwenden.

+0

Betrachten hält, dass das Bild größer sein wird, wenn sie von JPEG zu PNG Übergang. – JoxTraex

+1

Ja, das wird ein Kompromiss sein, wenn Sie nicht die Qualität verlieren wollen. –

+0

kann auch ein Problem auftreten, das dazu führt, dass das Bild bei Verwendung der Zeile kleiner wird: cropIntent.putExtra ("outputX", Math.round (screenWidth/dipToPixel) -10); – saner

1

Verwenden Sie diese Bibliothek, verwaltet diese Bibliothek beschnittenen Bildqualität als gut es sowohl Bild obwohl Crop Library

Verwandte Themen