2

我從代碼https://github.com/josnidhin/Android-Camera-Example專用相機應用程序在Android的問題 - 旋轉270,拉伸捕獲視圖,而不是越來越

幫助所有功能,但面臨着類似的一些問題:

  1. 如何添加自動的功能焦點和閃光。 - 在上次編輯的代碼中解決
  2. 當屏幕方向爲270時,實際視圖顯示180度旋轉。
  3. 捕獲視圖顯示拉伸。

CODE從那裏我所謂的自定義相機類

Camera cam = Camera.open(); 
    if (null == cam) 
    { 
     // no camera exists 
     Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
     startActivityForResult(i, RESULT_LOAD_IMAGE); 
    } 
    else 
    { 
     cam.release(); 
     Intent intent = new Intent(CreateList.this, CamTestActivity.class); 
     startActivityForResult(intent, TAKE_PICTURE); 
    } 

專用相機類CamTestActivity.class

import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 

import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.hardware.Camera; 
import android.hardware.Camera.AutoFocusCallback; 
import android.hardware.Camera.PictureCallback; 
import android.hardware.Camera.ShutterCallback; 
import android.os.Bundle; 
import android.os.Environment; 
import android.util.Log; 
import android.view.SurfaceView; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.View.OnLongClickListener; 
import android.view.ViewGroup.LayoutParams; 
import android.view.Window; 
import android.view.WindowManager; 
import android.widget.Button; 
import android.widget.FrameLayout; 

import com.example.R; 

public class CamTestActivity extends Activity { 
    private static final String TAG = "CamTestActivity"; 
    Preview preview; 
    Button buttonClick, buttonCancel; 
    Camera camera; 
    String fileName; 
    Activity act; 
    Context ctx; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     ctx = this; 
     act = this; 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); 

     setContentView(R.layout.take_picture); 

     preview = new Preview(this, (SurfaceView) findViewById(R.id.surfaceView)); 
     preview.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 
     ((FrameLayout) findViewById(R.id.preview)).addView(preview); 
     preview.setKeepScreenOn(true); 

     buttonClick = (Button) findViewById(R.id.buttonClick); 
     buttonCancel = (Button) findViewById(R.id.buttonCancel); 

     buttonClick.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       camera.takePicture(shutterCallback, rawCallback, jpegCallback); 
      } 
     }); 

     buttonClick.setOnLongClickListener(new OnLongClickListener() { 
      @Override 
      public boolean onLongClick(View arg0) { 
       camera.autoFocus(new AutoFocusCallback() { 
        @Override 
        public void onAutoFocus(boolean focus_flag, Camera cam) { 
         if (focus_flag) 
          cam.takePicture(shutterCallback, rawCallback, jpegCallback); 
        } 
       }); 
       return true; 
      } 
     }); 

     buttonCancel.setOnClickListener(new OnClickListener() { 
      public void onClick(View v)  { 
       finish(); 
      } 
     }); 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     // preview.camera = Camera.open(); 
     camera = Camera.open(0); 
     camera.startPreview(); 
     preview.setCamera(camera); 
    } 

    @Override 
    protected void onPause() { 
     if (camera != null)  { 
      camera.stopPreview(); 
      preview.setCamera(null); 
      camera.release(); 
      camera = null; 
     } 
     super.onPause(); 
    } 

    private void resetCam() { 
     camera.startPreview(); 
     preview.setCamera(camera); 

     Intent i = new Intent(); 
     i.putExtra("captured_img", fileName); 
     setResult(RESULT_OK, i); 
     finish(); 
    } 

    ShutterCallback shutterCallback = new ShutterCallback()  { 
     public void onShutter() { 
      // Log.d(TAG, "onShutter'd"); 
     } 
    }; 

    PictureCallback rawCallback = new PictureCallback()  { 
     public void onPictureTaken(byte[] data, Camera camera) { 
      // Log.d(TAG, "onPictureTaken - raw"); 
     } 
    }; 

    PictureCallback jpegCallback = new PictureCallback() { 
     public void onPictureTaken(byte[] data, Camera camera) { 
      FileOutputStream outStream = null; 
      try  { 
       // Write to SD Card 
       fileName = String.format(Environment.getExternalStorageDirectory() + "/FansRave_UploadDeal_Pic.jpg"); 

       outStream = new FileOutputStream(fileName); 
       outStream.write(data); 
       outStream.close(); 
       Log.d(TAG, "onPictureTaken - wrote bytes: " + data.length); 

       resetCam(); 
      } catch (FileNotFoundException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      Log.d(TAG, "onPictureTaken - jpeg"); 
     } 
    }; 
} 

Preview.class

import java.io.IOException; 
import java.util.List; 

import android.content.Context; 
import android.content.pm.PackageManager; 
import android.content.res.Configuration; 
import android.hardware.Camera; 
import android.hardware.Camera.Parameters; 
import android.hardware.Camera.Size; 
import android.util.Log; 
import android.view.SurfaceHolder; 
import android.view.SurfaceView; 
import android.view.View; 
import android.view.ViewGroup; 

class Preview extends ViewGroup implements SurfaceHolder.Callback { 
    private final String TAG = "Preview"; 

    SurfaceView mSurfaceView; 
    SurfaceHolder mHolder; 
    Size mPreviewSize; 
    List<Size> mSupportedPreviewSizes; 
    Camera mCamera; 
    Context mContext; 

    Preview(Context context, SurfaceView sv) { 
     super(context); 

     mContext = context; 
     mSurfaceView = sv; 

     mHolder = mSurfaceView.getHolder(); 
     mHolder.addCallback(this); 
     mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 
    } 

    public void setCamera(Camera camera) { 
     mCamera = camera; 
     if (mCamera != null) { 
      mSupportedPreviewSizes = mCamera.getParameters().getSupportedPreviewSizes(); 
      requestLayout(); 

      // get Camera parameters 
      Camera.Parameters params = mCamera.getParameters(); 

      List<String> focusModes = params.getSupportedFocusModes(); 
      if (focusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO)) { 
       // set the focus mode 
       params.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO); 
       // set Camera parameters 
       mCamera.setParameters(params); 
      } 
     } 
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     // We purposely disregard child measurements because act as a 
     // wrapper to a SurfaceView that center the camera preview instead 
     // of stretching it. 

     final int width = resolveSize(getSuggestedMinimumWidth(), widthMeasureSpec); 
     final int height = resolveSize(getSuggestedMinimumHeight(), heightMeasureSpec); 
     setMeasuredDimension(width, height); 

     if (mSupportedPreviewSizes != null) { 
      mPreviewSize = getOptimalPreviewSize(mSupportedPreviewSizes, width, height); 
     } 
    } 

    @Override 
    protected void onLayout(boolean changed, int l, int t, int r, int b) { 
     if (changed && getChildCount() > 0) { 
      final View child = getChildAt(0); 

      final int width = r - l; 
      final int height = b - t; 

      int previewWidth = width; 
      int previewHeight = height; 

      if (mPreviewSize != null) { 
       previewWidth = mPreviewSize.width; 
       previewHeight = mPreviewSize.height; 
      } 

      // Center the child SurfaceView within the parent. 
      if (width * previewHeight > height * previewWidth) { 
       final int scaledChildWidth = previewWidth * height/previewHeight; 
       child.layout((width - scaledChildWidth)/2, 0, (width + scaledChildWidth)/2, height); 
      } else { 
       final int scaledChildHeight = previewHeight * width/previewWidth; 
       child.layout(0, (height - scaledChildHeight)/2, width, (height + scaledChildHeight)/2); 
      } 
     } 
    } 

    public void surfaceCreated(SurfaceHolder holder) { 
     // The Surface has been created, acquire the camera and tell it where to draw. 
     try { 
      if (mCamera != null) { 
       if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { 
        Global.orientation = "LANDSCAPE"; 
        Log.e("Orientation mode", "" + Global.orientation); 
       } else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)  { 
        mCamera.stopPreview(); 
        mCamera.setDisplayOrientation(90); 
        mCamera.startPreview(); 
        Global.orientation = "PORTRAIT"; 
        Log.e("Orientation mode", "" + Global.orientation); 
       } 
       mCamera.setPreviewDisplay(holder); 
      } 
     } catch (IOException exception) { 
      Log.e(TAG, "IOException caused by setPreviewDisplay()", exception); 
      mCamera.release(); 
      mCamera = null; 
     } 
    } 

    public void surfaceDestroyed(SurfaceHolder holder) { 
     // Surface will be destroyed when we return, so stop the preview. 
     if (mCamera != null) { 
      mCamera.stopPreview(); 
      mCamera.setPreviewCallback(null); 
      mCamera.release(); 
      mCamera = null; 
     } 
    } 

    private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) { 
     final double ASPECT_TOLERANCE = 0.2; 
     double targetRatio = (double) w/h; 
     if (sizes == null) return null; 

     Size optimalSize = null; 
     double minDiff = Double.MAX_VALUE; 

     int targetHeight = h; 

     // Try to find an size match aspect ratio and size 
     for (Size size : sizes) { 
      double ratio = (double) size.width/size.height; 
      if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue; 
      if (Math.abs(size.height - targetHeight) < minDiff) { 
       optimalSize = size; 
       minDiff = Math.abs(size.height - targetHeight); 
      } 
     } 

     // Cannot find the one match the aspect ratio, ignore the requirement 
     if (optimalSize == null) { 
      minDiff = Double.MAX_VALUE; 
      for (Size size : sizes) { 
       if (Math.abs(size.height - targetHeight) < minDiff) { 
        optimalSize = size; 
        minDiff = Math.abs(size.height - targetHeight); 
       } 
      } 
     } 
     return optimalSize; 
    } 

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { 
     if (mCamera != null) { 
      Camera.Parameters parameters = mCamera.getParameters(); 
      parameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height); 

      // for flash 
      if (mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)) 
       parameters.setFlashMode(Parameters.FLASH_MODE_AUTO); 

      requestLayout(); 

      mCamera.setParameters(parameters); 
      mCamera.startPreview(); 

      mCamera.autoFocus(null); 
     } 
    } 
} 

XML文件take_picture.xml

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

<FrameLayout 
    android:id="@+id/preview" 
    android:layout_width="fill_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" > 

    <SurfaceView 
     android:id="@+id/surfaceView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

    <RelativeLayout 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:orientation="vertical" > 

     <Button 
      android:id="@+id/buttonClick" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerHorizontal="true" 
      android:layout_centerVertical="true" 
      android:background="@drawable/button_gradient" /> 

     <Button 
      android:id="@+id/buttonCancel" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:layout_alignParentLeft="true" 
      android:text="Cancel" /> 
    </RelativeLayout> 
</FrameLayout> 

</LinearLayout> 
+0

這些都是真正的多個問題如果沒有一個完整的完美無瑕的相機實現,它很難給出答案。 – for3st

+0

@ for3st我在我的應用程序實現的,面對我的所有問題,所以如果可能,請提供至少1個回答我的問題 –

+0

@MihirTrivedi的,我也面臨着你解決了這個相同的problem.Have?請回復。 – jagdish

回答

1

如果你得到定製相機的問題,那麼請使用意向的攝像頭,下面的代碼,使圖像旋轉,可以

Bitmap bm = BitmapFactory.decodeStream(new FileInputStream(mFile), null, mBitmapFactoryOptions); 
      Bitmap bitmap = bm; 

      ExifInterface exif = new ExifInterface(ImagePath); 

      int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1); 

      Log.e("ExifInteface .........", "rotation =" + orientation); 

      // exif.setAttribute(ExifInterface.ORIENTATION_ROTATE_90, 90); 

      Log.e("orientation", "" + orientation); 
      Matrix m = new Matrix(); 

      if ((orientation == ExifInterface.ORIENTATION_ROTATE_180)) { 
       m.postRotate(180); 
       // m.postScale((float) bm.getWidth(), (float) 
       // bm.getHeight()); 
       // if(m.preRotate(90)){ 
       Log.e("in orientation", "" + orientation); 
       bitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), m, true); 
       return bitmap; 
      } else if (orientation == ExifInterface.ORIENTATION_ROTATE_90) { 
       m.postRotate(90); 
       Log.e("in orientation", "" + orientation); 
       bitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), m, true); 
       return bitmap; 
      } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) { 
       m.postRotate(270); 
       Log.e("in orientation", "" + orientation); 
       bitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), m, true); 
       return bitmap; 

      } 
+0

謝謝你Piyush。我嘗試過,但在所有設備上都不能正常工作。 –

+0

請告訴我你在哪個設備出現問題? – Piyush

相關問題