ByakuGallery icon indicating copy to clipboard operation
ByakuGallery copied to clipboard

Portrait images appear landcape

Open dmagen12 opened this issue 10 years ago • 3 comments

I've tried using a portrait image from resource (which I took from the camera) but it appears landscape no matter what i've tried... Is there a way to display it correctly? thanks

dmagen12 avatar Jun 12 '14 11:06 dmagen12

Hey @dmagen12. Well I can't say for sure what is the problem because you didn't give enough information. But I will guess that is an issue related to the orientation from the ExifInterface. When you decode this Bitmap without using the lib, it displays correctly?

diegocarloslima avatar Jul 02 '14 17:07 diegocarloslima

Thank you for your response. I've tried this- I've downloaded the files from https://github.com/recurser/exif-orientation-examples which contains images with all EXIF orientation flags. When I view them in an image viewer the orientation appears correctly. I've added them to the test app (GalleryViewPagerSampleActivity) and the images orientation appear incorrect using the gallery - all beside the first one (where there's no need to rotate). I've further tried to change the gallery as on TileBitmapDrawable/ondraw adding matrix and doing matrix.postrotate but it didn't work (at first the image looks rotated but than it rotated back). thanks

dmagen12 avatar Jul 10 '14 07:07 dmagen12

I've tried to modify the "TileBitmapDrawable" code as below, and it's work for this case:

  1. modify to "private TileBitmapDrawable(ImageView parentView, BitmapRegionDecoder decoder, Bitmap screenNail, int degree)",

    
    synchronized(decoder) {
        mRegionDecoder = decoder;
        if (degree == 0 || degree == 180) {
            mIntrinsicWidth = mRegionDecoder.getWidth();
            mIntrinsicHeight = mRegionDecoder.getHeight();
        }
        else {
            mIntrinsicWidth = mRegionDecoder.getHeight();
            mIntrinsicHeight = mRegionDecoder.getWidth();
        }
    }
    mDegree = degree; // add a member variables "mDegree" to TileBitmapDrawable
    
    mDecoderWorker = new DecoderWorker(this, mRegionDecoder, mDecodeQueue, degree);
    
    
  2. modify to TileBitmapDrawable function "public void draw(Canvas canvas)"

    
    for (int i = 0; i < horizontalTiles; i++) {
    
        for(int j = 0; j < verticalTiles; j++) {
    
            final int tileLeft = i * currentTileSize;
            final int tileTop = j * currentTileSize;
            final int tileRight = (i + 1) * currentTileSize <= mIntrinsicWidth ? (i + 1) * currentTileSize : mIntrinsicWidth;
            final int tileBottom = (j + 1) * currentTileSize <= mIntrinsicHeight ? (j + 1) * currentTileSize : mIntrinsicHeight;
            mTileRect.set(tileLeft, tileTop, tileRight, tileBottom);
    
            // add a member variables "mDecodeRect" to TileBitmapDrawable
            if (mDegree == 90) {
                final int decodeLeft = tileTop;
                final int decodeRight = tileBottom;
                final int decodeTop = mIntrinsicWidth - tileRight;
                final int decodeBottom = mIntrinsicWidth - tileLeft;
                mDecodeRect.set(decodeLeft, decodeTop, decodeRight, decodeBottom);
            }
            else if (mDegree == 180) {
                final int decodeLeft = mIntrinsicWidth - tileRight;
                final int decodeRight = mIntrinsicWidth - tileLeft;
                final int decodeTop = mIntrinsicHeight - tileBottom;
                final int decodeBottom = mIntrinsicHeight - tileTop;
                mDecodeRect.set(decodeLeft, decodeTop, decodeRight, decodeBottom);
            }
            else if (mDegree == 270) {
                final int decodeLeft = mIntrinsicHeight - tileBottom;
                final int decodeRight = mIntrinsicHeight - tileTop;
                final int decodeTop =tileLeft;
                final int decodeBottom = tileRight;
                mDecodeRect.set(decodeLeft, decodeTop, decodeRight, decodeBottom);
            }
            else {
                mDecodeRect.set(tileLeft, tileTop, tileRight, tileBottom);
            }
    
            if(Rect.intersects(mVisibleAreaRect, mTileRect)) {
    
                // add a param "Rect decodeRect" to Tile constructor
                final Tile tile = new Tile(mInstanceId, mTileRect, mDecodeRect, i, j, currentLevel);
                ... ... 
            }
    
            ... ... 
        }
    
        ... ... 
    }
    
    
  3. modify to InitializationTask function "protected TileBitmapDrawable doInBackground(Object... params)"

    ... ...
    int degree = 0;
    Integer exifOrientation = exif.getTagIntValue(ExifInterface.TAG_ORIENTATION);
    if (exifOrientation != null) {
        degree = ExifInterface.getRotationForOrientationValue(exifOrientation.shortValue());
    }
    if (degree != 0) {
        Matrix matrix = new Matrix();
        matrix.setRotate(degree);
        screenNail = Bitmap.createBitmap(screenNail, 0, 0, 
                screenNail.getWidth(), screenNail.getHeight(), matrix, true);
    }
    
    TileBitmapDrawable drawable = new TileBitmapDrawable(mImageView, decoder, screenNail, degree);
    
    return drawable;
    
    
  4. modify to DecoderWorker function "public void run()"

    ... ...
    
    if(bitmap == null) {
        continue;
    }
    
    if (mDegree != 0) {
        Matrix matrix = new Matrix();
        matrix.setRotate(mDegree);
        bitmap = Bitmap.createBitmap(bitmap, 0, 0, 
                bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    }
    
    synchronized(sBitmapCacheLock) {
        sBitmapCache.put(tile.getKey(), bitmap);
    }
    
    ... ...
    
    

##thanks for diegocarloslima 's helpful code.

gaop avatar Dec 17 '14 16:12 gaop