代码之家  ›  专栏  ›  技术社区  ›  ForeverNoob

Grafika的CameraCaptureActivity-相机预览因预览大小更改而失真

  •  4
  • ForeverNoob  · 技术社区  · 10 年前

    我提到了 Grafika's CameraCaptureActivity 以在同时显示相机预览的同时记录视频。

    我对代码所做的更改(相关的)是-

    用户单击按钮启动 PreferencesActivity ,选择首选相机分辨率(存储 width height 值),然后单击“后退”按钮返回 CameraCaptureActivity .

    openCamera 方法-代替硬编码 宽度 身高 我从中选择价值 SharedPreferences

    这个 AspectFrameLayout's 属性是这样设置的 android:background="@android:color/holo_blue_bright" 。因此预览与 AspectFrameLayout

    这个问题主要发生在大小从较大分辨率变为较小分辨率时。

    在较小的屏幕中(HTC One M7/Xperia SP) Distorted camera preview in HTC

    在更大的屏幕中(三星Galaxy Tab S/Nexus 9)- Distorted camera preview in Samsung Tab S

    我注意到这是因为 onSurfaceChanged 被打了两次电话。第一次使用错误的值 宽度 身高 第二次使用正确的值。

    然而,如果我离开活动并返回活动而不做任何更改,布局看起来很好。(喜欢去 首选项活动 单击“上一步”而不更改分辨率。)

    我已经设置了以下内容 AndroidManifest.xml -

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

    编辑:此问题仅在预览时出现。录制的视频大小正确。

    编辑2:这是我的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>
    

    我确信我传递了正确的值来设置视图的宽度和高度,因为如果我离开活动并返回活动而不做任何更改,预览设置正确。(如转到另一个活动并单击“上一步”返回 摄像头捕获活动 .)

    2 回复  |  直到 10 年前
        1
  •  1
  •   Preethi Rao    10 年前

    如果问题出在预览上,我不清楚你想说什么。。然后您可以在opencamera方法中使用以下代码

    在您的 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);
    }
    
        2
  •  1
  •   user4728480 user4728480    10 年前

    使用此项打开相机

    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高

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    
    Bitmap resizedBitmap = Bitmap.createScaledBitmap(originalBitmap, 200, 200, false);
    

    此方法不会使调整大小的图像变形。 希望有帮助。