前置摄像头拍摄时显示的图像,但后置摄像头拍摄时不可见

时间:2015-04-27 09:20:46

标签: android camera android-camera

我在我的应用中使用原生相机。拍完照片后,我会在Imageview的下一个活动中向用户展示。现在的问题是,当我保存前置摄像头拍摄的照片时,图片会显示在下一个活动的图像视图中,但不会出现在后置摄像头拍摄的情况下。

按照以下方式拍照后,我将进行下一个活动:

 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
         super.onActivityResult(requestCode, resultCode, data);

         if (resultCode == RESULT_OK) {


                 case REQUEST_CODE_HIGH_QUALITY_IMAGE:
                     Toast.makeText(getApplicationContext(),
                        sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
                                         Uri.parse("file://"
                                                         + Environment.getExternalStorageDirectory())));
                         //refreshing gallery
                         Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                         mediaScanIntent.setData(mHighQualityImageUri);
                         sendBroadcast(mediaScanIntent);

                         Intent intentActivity = new Intent(MyCameraActivity.this,PhotoSortrActivity.class);
                        intentActivity.putExtra("data", mHighQualityImageUri);
                        Log.v("Uri before Sending",mHighQualityImageUri+"");
                         startActivity(intentActivity);


                         break;
                 default:
                         break;
                 }

         }

这是我展示捕获图像的地方。 :

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_photosortr);
        this.setTitle(R.string.instructions);
    image = (ImageView) findViewById(R.id.img_view);


    InputStream iStream = null;
    try {
        iStream = getContentResolver().openInputStream(uri);
         inputData = getBytes(iStream);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    Bitmap cameraBitmap = BitmapFactory.decodeByteArray(inputData, 0, inputData.length);

    Bitmap cameraScaledBitmap = Bitmap.createScaledBitmap(cameraBitmap, cameraBitmap.getWidth(), cameraBitmap.getHeight(), true);
    Matrix matrix = new Matrix();
    if(cameraScaledBitmap.getWidth()>cameraScaledBitmap.getHeight())
    {
        matrix = new Matrix();
        matrix.postRotate(270);
    }
 //   final Bitmap newImage = Bitmap.createBitmap(cameraScaledBitmap.getWidth(), cameraScaledBitmap.getHeight(), Bitmap.Config.ARGB_8888);

    // ask the bitmap factory not to scale the loaded bitmaps
    BitmapFactory.Options opts = new BitmapFactory.Options();

    opts.inScaled = false;
   Bitmap cameraScaledBitmap2 = Bitmap.createBitmap(cameraScaledBitmap, 0, 0, cameraScaledBitmap.getWidth(), cameraScaledBitmap.getHeight(), matrix, true);
  //  image.setImageURI(uri);
    image.setImageBitmap(cameraScaledBitmap2);
    BitmapDrawable bg = new BitmapDrawable(cameraScaledBitmap2);

   // photoSorter.SetBackgroundFromUrl(data);

}

@Override
protected void onResume() {
    super.onResume();
    //photoSorter.loadImages(this);
}

@Override
protected void onPause() {
    super.onPause();
    //photoSorter.unloadImages();
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
        //photoSorter.trackballClicked();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}


public byte[] getBytes(InputStream inputStream) throws IOException {
    ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
    int bufferSize = 1024;
    byte[] buffer = new byte[bufferSize];

    int len = 0;
    while ((len = inputStream.read(buffer)) != -1) {
        byteBuffer.write(buffer, 0, len);
    }
    return byteBuffer.toByteArray();
}

以下是我的第二项活动布局:

 <?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/fl_camera">


    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        >



        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:contentDescription="content_desc_overlay"
            android:src="@drawable/ic_launcher"
            android:id="@+id/img_view"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            />
    </RelativeLayout>

</FrameLayout>

为什么在使用后置摄像头时没有在Imageview中设置图像,而在前置摄像头拍摄时它正在工作。请帮帮我

2 个答案:

答案 0 :(得分:0)

Bitmap myBitmap = BitmapFactory.decodeFile(mediaFile.getAbsolutePath());
int height = (myBitmap.getHeight() * 512 / myBitmap.getWidth());
Bitmap scale = Bitmap.createScaledBitmap(myBitmap, 512, height, true);

//这里mediaFile是图像的路径。 //将比例位图显示到ImageView

答案 1 :(得分:0)

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class ImageResizer {

public static Bitmap decodeSampledBitmapFromFile(String filename,
   int reqWidth, int reqHeight) {
     // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options
    options = new BitmapFactory.Options();
  options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filename, options);
    // Calculate inSampleSize
      options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
        // Decode bitmap with inSampleSize set
          options.inJustDecodeBounds = false;
          return BitmapFactory.decodeFile(filename, options);
         }

        public static int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
     // BEGIN_INCLUDE (calculate_sample_size)
    // Raw height and width of image
  final int height = options.outHeight;
     final int width = options.outWidth;
   int inSampleSize = 1;

   if (height > reqHeight || width > reqWidth) {

final int halfHeight = height / 2;
final int halfWidth = width / 2;

// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) > reqHeight
        && (halfWidth / inSampleSize) > reqWidth) {
    inSampleSize *= 2;
}

// This offers some additional logic in case the image has a strange
// aspect ratio. For example, a panorama may have a much larger
// width than height. In these cases the total pixels might still
// end up being too large to fit comfortably in memory, so we should
// be more aggressive with sample down the image (=larger inSampleSize).

long totalPixels = width * height / inSampleSize;

// Anything more than 2x the requested pixels we'll sample down further
final long totalReqPixelsCap = reqWidth * reqHeight * 2;

while (totalPixels > totalReqPixelsCap) {
    inSampleSize *= 2;
    totalPixels /= 2;
     }
       }
 return inSampleSize;
       // END_INCLUDE (calculate_sample_size)
        }

        }

方法的用法

   Bitmap bmp = ImageResizer.decodeSampledBitmapFromFile(new  File(filePath).getAbsolutePath(), 512, 342); 

这将调整你的位图大小,以便你可以摆脱OOM error.process这些内部UI线程似乎更好。