Grafika CameraCaptureActivity - camera preview distorted when resizing previews

I referenced Grafika CameraCaptureActivity for recording video while displaying a camera preview.

Changes made by me to the code (which is relevant), -

The user presses the button to start PreferencesActivity, selects the preferred camera resolution (which stores the values ​​of widthand height) and presses the "Back" button to return to CameraCaptureActivity.

In the method openCamera. Instead of hard coding widthand heightI select values ​​fromSharedPreferences

The attribute is AspectFrameLayout'sset this way android:background="@android:color/holo_blue_bright". Thus, the preview matches the sizeAspectFrameLayout

This problem occurs mainly when the size changes from a larger resolution to a smaller one.

On small screens (HTC One M7 / Xperia SP) Distorted camera preview in HTC

On large screens (Samsung Galaxy Tab S / Nexus 9) - Distorted camera preview in Samsung Tab S

I noticed that this happens because it onSurfaceChangedis called twice. The first time with incorrect values ​​for widthand height, and the second time with the correct values.

However, the layout looks great if I leave the activity and return to it without making any changes. (Like going to PreferencesActivityand clicking "Back" without changing the resolution.)

I set the following to AndroidManifest.xml-

<activity android:name=".activity.camera.CameraCaptureActivity"
 android:launchMode="singleInstance"
 android:screenOrientation="landscape"
 android:theme="@android:style/Theme.Holo.Light.NoActionBar">
</activity>

EDIT: This problem only occurs when previewing. The recorded video is the correct size.

EDIT 2: This is my layout.xml

    <RelativeLayout xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/cameracapturescreen_rootparent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"
tools:context="com.myapp.CameraCaptureActivity">

<com.myapp.activity.camera.PreviewFrameLayout
    android:id="@+id/surfaceview_framelayout"
    android:layout_width="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:background="@android:color/holo_blue_bright"
    android:layout_height="wrap_content">

    <android.opengl.GLSurfaceView
        android:id="@+id/cameraPreview_surfaceView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="left"/>

</com.myapp.activity.camera.PreviewFrameLayout>

, , , , , . ( "" , CameraCaptureActivity.)

+4
2

, , ..

openCamera(), ... ,

    if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
        mCamera.setDisplayOrientation(90);
    } else {
        mCamera.setDisplayOrientation(180);
    }

handleSetSurfaceTexture()

    if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
        mCamera.setDisplayOrientation(90);
     } else {
        mCamera.setDisplayOrientation(180);
    }
          Camera.Parameters parms = mCamera.getParameters();

          CameraUtils.choosePreviewSize(parms, mCameraPreviewWidth, mCameraPreviewHeight);

          // Give the camera a hint that we're recording video.  This   can have a big
          // impact on frame rate.
          parms.setRecordingHint(true);
          mCamera.setParameters(parms);

onPause()

InputMethodManager imm = (InputMethodManager)getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(((EditText)findViewById(R.id.username)).getWindowToken(), 0);

if(imm.isAcceptingText()) {
Intent intent = new Intent(this, CameraCaptureActivity.class);
startActivity(intent);
}
+1

Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);    
i.putExtra(MediaStore.EXTRA_OUTPUT, MyFileContentProvider.CONTENT_URI); 
startActivityForResult(i, CAMERA_RESULT);

MyFileContentProvider

public class MyFileContentProvider extends ContentProvider {

 public static final Uri CONTENT_URI = Uri.parse("content://com.algorithmapps.dmsimagesave/");

 private static final HashMap<String, String> MIME_TYPES = new         HashMap<String, String>();

 static {

  MIME_TYPES.put(".jpg", "image/jpeg");

  MIME_TYPES.put(".jpeg", "image/jpeg");

 }

 @Override

 public boolean onCreate() {

  try {

   File mFile = new File(getContext().getFilesDir(), "newImage.jpg");

   if(!mFile.exists()) {

mFile.createNewFile();

   }

   getContext().getContentResolver().notifyChange(CONTENT_URI, null);

   return (true);

  } catch (Exception e) {

   e.printStackTrace();

   return false;

  }

 }

 @Override

 public String getType(Uri uri) {

  String path = uri.toString();

  for (String extension : MIME_TYPES.keySet()) {

   if (path.endsWith(extension)) {

    return (MIME_TYPES.get(extension));

   }

  }

  return (null);

 }

 @Override

 public ParcelFileDescriptor openFile(Uri uri, String mode)

 throws FileNotFoundException {

  File f = new File(getContext().getFilesDir(), "newImage.jpg");

  if (f.exists()) {

   return (ParcelFileDescriptor.open(f,

     ParcelFileDescriptor.MODE_READ_WRITE));

  }

  throw new FileNotFoundException(uri.getPath());

 }

 @Override

 public Cursor query(Uri url, String[] projection, String selection,

   String[] selectionArgs, String sort) {

  throw new RuntimeException("Operation not supported");

 }

 @Override

 public Uri insert(Uri uri, ContentValues initialValues) {

  throw new RuntimeException("Operation not supported");

 }

 @Override

 public int update(Uri uri, ContentValues values, String where,

   String[] whereArgs) {

  throw new RuntimeException("Operation not supported");

 }

 @Override

 public int delete(Uri uri, String where, String[] whereArgs) {

  throw new RuntimeException("Operation not supported");

 }

} 

onActivityResultMethod ,

 if (resultCode == RESULT_OK && requestCode == CAMERA_RESULT) {

       File out = new File(getFilesDir(), "newImage.jpg");

         if(!out.exists()) {

                Toast.makeText(getBaseContext(),

                  "Error while capturing image", Toast.LENGTH_LONG)

                  .show();


       }             
          originalBitmap = BitmapFactory.decodeFile(out.getAbsolutePath());   
    }

, , 200 * 200 height

BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;

Bitmap resizedBitmap = Bitmap.createScaledBitmap(originalBitmap, 200, 200, false);

. , .

+1

All Articles