gpt4 book ai didi

android - 为什么使用 ImageLoader 显示的肖像图像是旋转的?

转载 作者:行者123 更新时间:2023-11-29 00:17:55 26 4
gpt4 key购买 nike

我正在尝试使用 ImageAdapter 和 ImageLoader 将图片库显示为 GridView。不过,所有肖像图像都旋转了 +-90 度。我知道 getExifOrientation 代码来检查图像方向并将其旋转回来,但我只是不知道在哪里使用它..

这是我使用的 ImageAdapter 类,大部分内容都在这里,并尝试正确使用它。

public class ImageAdapter extends BaseAdapter {

ArrayList<String> _list;
LayoutInflater _inflater;
Context _context;

public ImageAdapter(Context context, ArrayList<String> imageList) {

_context = context;
_inflater = LayoutInflater.from(_context);
_list = new ArrayList<String>();
this._list = imageList;
}

@Override
public int getCount() {
return _imageUrls.size();
}

@Override
public Object getItem(int position) {
return null;
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

if(convertView == null) {
convertView = _inflater.inflate(R.layout.multiphoto_item, null);
}

final ImageView imageView = (ImageView) convertView.findViewById(R.id.image_view);
imageView.setTag(position);
imageView.setScaleType(ScaleType.CENTER);

_imageLoader.displayImage("file://"+_imageUrls.get(position), imageView, _options, new SimpleImageLoadingListener() {
@Override
public void onLoadingComplete(Bitmap loadedImage) {
Animation anim = AnimationUtils.loadAnimation(ViewCategoryActivity.this, R.anim.fade_in);
imageView.setAnimation(anim);
anim.start();
}


});

imageView.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

Integer position = (Integer)imageView.getTag();
String url = _imageUrls.get(position);
Intent intent = new Intent("com.eibimalul.smartgallery.SingleImageDisplay");
intent.putExtra("selectedImagePosition", position);
intent.putStringArrayListExtra("imageUrls", _imageUrls);
intent.putExtra("Title", _activityTitle);
startActivityForResult(intent, 0);
}
});

return convertView;
}
}

然后我将它连接到 GridView:

_options = new DisplayImageOptions.Builder()
.showStubImage(R.drawable.stub_image)
.showImageForEmptyUri(R.drawable.image_for_empty_url)
.cacheInMemory()
.cacheOnDisc()
.build();

_imageAdapter = new ImageAdapter(this, _imageUrls);

GridView gridView = (GridView) findViewById(R.id.gridview);
gridView.setAdapter(_imageAdapter);

任何帮助将不胜感激..

最佳答案

不确定,但对我来说这可行,但我认为这不是解决此问题的最佳方法,但它会有所帮助。

mImageLoader.displayImage(photoBean.getPhotoURL(), imageView, options, new ImageRotationListener());

    options = new DisplayImageOptions.Builder()
.cacheOnDisk(true)
.cacheInMemory(true)
.considerExifParams(true)
.bitmapConfig(Bitmap.Config.RGB_565)
.showImageForEmptyUri(R.drawable.icon_venue_default)
.showImageOnLoading(R.drawable.icon_venue_default)
.showImageOnFail(R.drawable.icon_venue_default)
.build();

package com.urbanft.utils;

import android.graphics.Bitmap;
import android.media.ExifInterface;
import android.view.View;
import android.widget.ImageView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;

import java.io.File;

/**
* Created by kiwitech on 30/8/16.
*/
public class ImageRotationListener implements ImageLoadingListener {

@Override
public void onLoadingStarted(String imageUri, View view) {
}

@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
}

@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
try {
if(loadedImage != null){
File file = ImageLoader.getInstance().getDiskCache().get(imageUri);
if(file == null){
return;
}
int rotation = getCameraPhotoOrientation(file);
Bitmap bitmap = null;
if(rotation != 0){
bitmap = resizeBitmap(loadedImage, 1000, 1000);
if(bitmap != null){
if(view instanceof ImageView){
((ImageView) view).setImageBitmap(bitmap);
}
}
}
view.setBackgroundDrawable(null);
}
}
catch (Exception e) {
e.printStackTrace();
}
}

@Override
public void onLoadingCancelled(String imageUri, View view) {
}

public Bitmap resizeBitmap( Bitmap input, int destWidth, int destHeight )
{
int srcWidth = input.getWidth();
int srcHeight = input.getHeight();
boolean needsResize = false;
float p;
if (srcWidth > destWidth || srcHeight > destHeight) {
needsResize = true;
if ( srcWidth > srcHeight && srcWidth > destWidth) {
p = (float)destWidth / (float)srcWidth;
destHeight = (int)( srcHeight * p );
}
else {
p = (float)destHeight / (float)srcHeight;
destWidth = (int)( srcWidth * p );
}
}
else {
destWidth = srcWidth;
destHeight = srcHeight;
}
if (needsResize) {
Bitmap output = Bitmap.createScaledBitmap( input, destWidth, destHeight, true );
return output;
}
else {
return input;
}
}

public int getCameraPhotoOrientation(File imageFile) throws Exception {
int rotate = 0;
try {
if(imageFile.exists()){
ExifInterface exif = new ExifInterface(imageFile.getAbsolutePath());
int orientation = Integer.parseInt(exif.getAttribute(ExifInterface.TAG_ORIENTATION));
switch (orientation) {
case ExifInterface.ORIENTATION_NORMAL:
rotate = 0;
break;

case ExifInterface.ORIENTATION_ROTATE_270:
rotate = 270;
break;

case ExifInterface.ORIENTATION_ROTATE_180:
rotate = 180;
break;

case ExifInterface.ORIENTATION_ROTATE_90:
rotate = 90;
break;
}
}
}
catch (Exception e) {
e.printStackTrace();
}
return rotate;
}
}

关于android - 为什么使用 ImageLoader 显示的肖像图像是旋转的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25250274/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com