Recadrage image Android

Recadrage image Android - Android - Technologies Mobiles

Marsh Posté le 30-05-2013 à 15:34:44    

Bonjour tout le monde je travail sur une appli android qui fait le recadrage d'une image j'ai cherché un peu a droite a gauche sur internet et livre et voila ce que j'ai trouvé voir le code en dessous:  
 le probleme c'est que : eclipse me signale une erreur au niveau des portions de code en rouge (TextureImageDetailsActivity) et je sais pas comment les resoudre aidez mois svp a les resoudres  merci infiniment   :jap:   :jap:   :jap:   :jap:   :jap:  
 
 
Code :
class ImageViewWithRectangle extends ImageView {
// Enable or not the cropping rectangle
private boolean hasCropRectangle = false;
// Cropping rectangle that we are drawing over the image
private RectF croppingRect = new RectF();
private RectF intialcroppingRect;
private boolean cropRectInitialized = false;
// Turns this boolean to false when we touch inside it
private boolean moveRectangle = false;
// Turns this boolean to true when we touch the border of the rectangle
private boolean resizeRectangle = false;
// Index of the corner that the user is currently dragging
private int sideIndex = -1;
// Maximum distance from a side to consider that we are touching it
private final float MAXDIST = 20;
// Minimum size of the cropping rectangle
private final int MINSIZE = 5;
// Values to deal with the touching
private float oldTouchValueX;
private float oldTouchValueY;
// This is the bounds of the drawable inside the image view
private RectF drawableRect;
TextureImageDetailsActivity parentContext;
public ImageViewWithRectangle(Context context, AttributeSet attrs) {
 super(context, attrs);
 intialcroppingRect = new RectF(20f, 20f, 80f, 80f);
}
// Set the parent so we can tell it when the crop rectangle is moved
public void setParentContext(TextureImageDetailsActivity context) {
 parentContext = context;
}
@Override
public void setImageDrawable (Drawable drawable) {
 super.setImageDrawable(drawable);
 intialcroppingRect.set(20f, 20f, 80f, 80f);
}
public void setIsCropped(boolean cropRectangleActivated) {
 hasCropRectangle = cropRectangleActivated;
 cropRectInitialized = false;
 // Invalidate the view so show or hide the rectangle
 invalidate();
}
// Set the cropping rectangle by given value in percentage
public void setCroppingRectangle(RectF cropRect) {
 intialcroppingRect = cropRect;
 cropRectInitialized = false;
}
public RectF getCroppingRectangle() {
 return croppingRect;
}
@Override
protected void onDraw (Canvas canvas) {
 // Let the image draw itself first
 super.onDraw(canvas);
 if (hasCropRectangle) {
  // If we draw the image for the first time we need to initialize the crop rectangle.
  if (!cropRectInitialized) {
   computeDrawableRect();
   float widthratio = drawableRect.width()/100f;
   float heightratio = drawableRect.height()/100f;
   croppingRect.set(intialcroppingRect.left*widthratio + drawableRect.left,
        intialcroppingRect.top*heightratio  + drawableRect.top,
        intialcroppingRect.right*widthratio + drawableRect.left,
        intialcroppingRect.bottom*heightratio + drawableRect.top);
   cropRectInitialized = true;
  }
  // Creates a paint object and sets its attributes
  Paint paint = new Paint();
  // Depending if we move the rectangle or not, we use a different color
  if (moveRectangle)
   paint.setColor(Color.GREEN);
  else
   paint.setColor(Color.RED);
  paint.setStrokeWidth(3);
  paint.setStyle(Style.STROKE);
  // Draw a rectangle
  canvas.drawRect(croppingRect, paint);
  // Set the color to green to draw the selected side
  paint.setColor(Color.GREEN);
  // Change the color of the touched side if we are in the operation of resizing the rectangle
  if (sideIndex == 0)
   // Top side
   canvas.drawLine(croppingRect.left, croppingRect.top, croppingRect.right, croppingRect.top, paint);
  else if (sideIndex == 1)
   //  Right side
   canvas.drawLine(croppingRect.right, croppingRect.top, croppingRect.right, croppingRect.bottom, paint);
  else if (sideIndex == 2)
   // Bottom side
   canvas.drawLine(croppingRect.right, croppingRect.bottom, croppingRect.left, croppingRect.bottom, paint);
  else if (sideIndex == 3)
   // Left side
   canvas.drawLine(croppingRect.left, croppingRect.bottom, croppingRect.left, croppingRect.top, paint);
  // From here we darken the outside of the cropping rectangle to  
  // give a better view of what is ketp.
  paint.setColor(Color.BLACK);
  paint.setAlpha(100);
  paint.setStyle(Paint.Style.FILL);
  // Create a path with the imageview bounds minus the cropping rectangle.
  Path path = new Path();
  path.addRect(0, 0, getWidth(), getHeight(), Path.Direction.CW);
  path.addRect(croppingRect, Path.Direction.CCW);
  path.setFillType(Path.FillType.EVEN_ODD);
  // Draw this path.
  canvas.drawPath(path, paint);
 }
}
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev){
        super.dispatchTouchEvent(ev);
        return onTouchEvent(ev);
    }
    // Calculate drawable rect
    private void computeDrawableRect() {
   
     // Ratio of the imageView
 float imageRatio = (float)getWidth()/getHeight();
 float drawableRatio = (float)getDrawable().getIntrinsicWidth()/getDrawable().getIntrinsicHeight();
 float dWidth = getDrawable().getIntrinsicWidth();
 float dHeight = getDrawable().getIntrinsicHeight();
 drawableRect = new RectF();
 // If the images have the same ratio, the drawable rect is the same than the image view
 if (imageRatio == drawableRatio)
  drawableRect.set(0, 0, getWidth(), getHeight());
 // If the width is the biggest side in both image
 else if (imageRatio < drawableRatio) {
  // Get the ratio between the two width
  float ratio = dWidth/getWidth();
  // So calculate the new size of the height
  float height = dHeight/ratio;
  float diff = (getHeight() - height)/2f;
  // We have now the size of the scaled drawable
  drawableRect.set(0, diff, getWidth(), diff + height);
 }
 // If the height is the biggest side in both image
 else if (imageRatio > drawableRatio) {
  // Get the ratio between the two width
  float ratio = dHeight/getHeight();
  // So calculate the new size of the height
  float width = dWidth/ratio;
  float diff = (getWidth() - width)/2f;
  // We have now the size of the scaled drawable
  drawableRect.set(diff, 0, diff + width, getHeight());
 }
    }
   
@Override
public boolean onTouchEvent(MotionEvent event) {
 // Reimplement the touch event to resize and move the position of the cropping rectangle
 switch (event.getAction())
 {
 case MotionEvent.ACTION_DOWN:
 {
  oldTouchValueX = event.getX();
  oldTouchValueY = event.getY();
  // Test if the finger touch one of the side
  // The side index goes from 0 to 3 with 0 as the top, 1 is the right,  
  //                  2 bottom and 3 left
  if (oldTouchValueX > croppingRect.left && oldTouchValueX < croppingRect.right
   && Math.abs(oldTouchValueY - croppingRect.top) < MAXDIST)
   sideIndex = 0;
  else if (oldTouchValueY > croppingRect.top && oldTouchValueY < croppingRect.bottom
    && Math.abs(oldTouchValueX - croppingRect.right) < MAXDIST)
   sideIndex = 1;
  else if (oldTouchValueX > croppingRect.left && oldTouchValueX < croppingRect.right
    && Math.abs(oldTouchValueY - croppingRect.bottom) < MAXDIST)
   sideIndex = 2;
  else if (oldTouchValueY > croppingRect.top && oldTouchValueY < croppingRect.bottom
    && Math.abs(oldTouchValueX - croppingRect.left) < MAXDIST)
   sideIndex = 3;
  if (sideIndex != -1)
   resizeRectangle = true;
  else if (croppingRect.contains(oldTouchValueX, oldTouchValueY))
   moveRectangle = true;
  // Invalidate the view so the rectangle is getting redraw
  invalidate();
  return resizeRectangle || moveRectangle;
 }
 case MotionEvent.ACTION_MOVE:
 {
  // Get the new position of the finger
  float currentX = event.getX();
  float currentY = event.getY();
  if (hasCropRectangle) {
   // If in the right mode, moves the rectangle by the difference  
   if (moveRectangle) {
    float offsetX = currentX-oldTouchValueX;
    float offsetY = currentY-oldTouchValueY;
    // Check if the offset will not make the rectangle to go outside of the view
    if (croppingRect.right + offsetX > drawableRect.right)
     offsetX = 0;
    else if (croppingRect.left + offsetX < drawableRect.left)
     offsetX = 0;
    if (croppingRect.top + offsetY < drawableRect.top)
     offsetY = 0;
    else if (croppingRect.bottom + offsetY > drawableRect.bottom)
     offsetY = 0;
    croppingRect.offset(offsetX, offsetY);
    // Invalidate the view so the rectangle is getting redraw
    invalidate();
   }
   else if (resizeRectangle) {
    float width = drawableRect.right;
    float height = drawableRect.bottom;
    // Depending on the side we are currently touching,  
    // we change the coordinate of the appropriate parameter
    switch (sideIndex) {
    case 0 : if (currentY > drawableRect.top && currentY < height && currentY < (croppingRect.bottom - MINSIZE)) croppingRect.top = currentY;
       break;
    case 1 : if (currentX > drawableRect.left && currentX < width && croppingRect.left < (currentX - MINSIZE)) croppingRect.right = currentX;
       break;
    case 2 : if (currentY > drawableRect.top && currentY < height && croppingRect.top < (currentY - MINSIZE)) croppingRect.bottom = currentY;
            break;
    case 3 : if (currentX > drawableRect.left && currentX < width && currentX < (croppingRect.right - MINSIZE)) croppingRect.left = currentX;
       break;
    }
    // Invalidate the view so the rectangle is getting redraw
    invalidate();
   }
  }
  // Register the position of the finger at the end of the move
  oldTouchValueX = currentX;
  oldTouchValueY = currentY;
  return true;
 }
 case MotionEvent.ACTION_UP:
 {
  // When the finger is lift, we turn every boolean to false
  moveRectangle = false;
  resizeRectangle = false;
  sideIndex = -1;
  // Tells the parent the rectangle was moved
  parentContext.setCropRect();
  // Invalidate the view so the rectangle is getting redraw
  invalidate();
  return true;
 }
 }
 return super.onTouchEvent(event);
}
// Returns the ratio width/height of the crop rectangle
public float getRatio() {
 return croppingRect.width()/croppingRect.height();
}
// Returns the cropping rectangle in percentage of the width and height
RectF getCropRectInBitmapCoordinate() {
 if (drawableRect == null)
  return intialcroppingRect;
 float widthratio = 100f/drawableRect.width();
 float heightratio = 100f/drawableRect.height();
 RectF cropRect = new RectF((croppingRect.left - drawableRect.left)*widthratio,
        (croppingRect.top - drawableRect.top)*heightratio,
        (croppingRect.right - drawableRect.left)*widthratio,
        (croppingRect.bottom - drawableRect.top)*heightratio);
 return cropRect;
}
// Returns a bitmap which is cropped with the cropping rectangle
public Bitmap getBitmap() {
 if (getDrawable() == null)
  return null;
 // Return the complete image or the cropped rectangle
 else if (hasCropRectangle) {
  Bitmap bitmap = ((BitmapDrawable)getDrawable()).getBitmap();
  return Bitmap.createBitmap(bitmap,
    Math.round(croppingRect.left), Math.round(croppingRect.top),
    Math.round(croppingRect.width()), Math.round(croppingRect.height()));
 }
 else
  return ((BitmapDrawable)getDrawable()).getBitmap();
}
}

Reply

Marsh Posté le 30-05-2013 à 15:34:44   

Reply

Marsh Posté le 30-05-2013 à 15:46:51    

Ce sujet a été déplacé de la catégorie OS Alternatifs vers la categorie Technologies Mobiles par O'gure


---------------
Relax. Take a deep breath !
Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed