简体   繁体   English

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

[英]Image shown when taken by front camera but not visible when taken by Back camera

I am using native camera in my app. 我在我的应用程序中使用本地相机。 And after taking picture I am showing it to user on next activity in the Imageview. 拍照后,我将在Imageview中的下一个活动中向用户展示它。 Now the problem is, when I save picture taken by front camera, the picture shows up in the next activity's imageview but not in the case when taken by back camera. 现在的问题是,当我保存由前置摄像头拍摄的照片时,该图片显示在下一个活动的图像视图中,而不是由后置摄像头拍摄的情况。

I am going to next activity after taking picture in the following way: 我将通过以下方式拍照后再进行下一个活动:

 @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;
                 }

         }

and this where I am showing the captured image. 这是我显示捕获的图像的地方。 :

@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();
}

Here is my layout of second activity: 这是我第二项活动的布局:

 <?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>

Why it is not setting image in the Imageview when using backcamera whereas it is working when taken by front camera. 为什么在使用后置摄像头时不能在Imageview中设置图像,而在使用前置摄像头拍摄时却可以工作。 please help me 请帮我

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

// Here mediaFile is path of image. //这里的mediaFile是图像的路径。 // display scale bitmap to your ImageView //将比例位图显示到ImageView

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)
        }

        }

Usage of method 方法的使用

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

This will resize your bitmap so that you can get rid from OOM error.process these inside UI thread which seems better. 这将调整您的位图大小,以便您摆脱OOM错误。在UI线程中处理这些似乎更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM