Note that there are some explanatory texts on larger screens.

plurals
  1. POCustom camera app in android problems - rotation 270, stretched capture view and not getting all functions
    text
    copied!<p>I got the help from code <a href="https://github.com/josnidhin/Android-Camera-Example" rel="nofollow">https://github.com/josnidhin/Android-Camera-Example</a></p> <p>But facing some problems like:</p> <ol> <li>How to add functionalities of auto focus and flash. - <strong><em>solved in last edited code</em></strong></li> <li>The when screen orientation is 270 so real view shows 180 degree rotated.</li> <li>Capture view is showing stretched.</li> </ol> <p>The <strong>CODE</strong> from where i called the custom camera class</p> <pre><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); } </code></pre> <p>Custom camera class <strong>CamTestActivity.class</strong></p> <pre><code>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"); } }; } </code></pre> <p><strong>Preview.class</strong></p> <pre><code>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&lt;Size&gt; 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&lt;String&gt; 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 &amp;&amp; getChildCount() &gt; 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 &gt; 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&lt;Size&gt; 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) &gt; ASPECT_TOLERANCE) continue; if (Math.abs(size.height - targetHeight) &lt; 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) &lt; 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); } } } </code></pre> <p>XML file <strong>take_picture.xml</strong></p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;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" &gt; &lt;FrameLayout android:id="@+id/preview" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1" &gt; &lt;SurfaceView android:id="@+id/surfaceView" android:layout_width="wrap_content" android:layout_height="wrap_content" /&gt; &lt;RelativeLayout android:layout_width="wrap_content" android:layout_height="fill_parent" android:orientation="vertical" &gt; &lt;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" /&gt; &lt;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" /&gt; &lt;/RelativeLayout&gt; &lt;/FrameLayout&gt; &lt;/LinearLayout&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload