Examples of android camera are explained in detail

  • 2020-08-22 22:46:27
  • OfStack

An example of camera is given in this paper. Share to everybody for everybody reference. The details are as follows:

1. About the 90 degrees of vertical and horizontal preview

Cause analysis,

After verification and experiments, it can be confirmed that the SDK (ES11en.hardware.Camera) provided by Android may not be able to load the camera with portrait properly. When the camera is loaded with portrait mode, the following situations will occur:

. Camera imaging left 90 degrees (tilt);

. Incorrect length-width ratio of the camera image (out of proportion).

Probably, because it can be solved by some more complicated means. If that's true, then it's obvious why portrait doesn't work properly. Why this might be the case, see the analysis below.

If android:screenOrientation="landscape" is not added to activity, then android:screenOrientation="portrait" (portrait) is default, the camera preview will turn 90 degrees to the left and lose ratio. The reason is that (I guess), camera control map is Android bottom, were fixed in landscape manner is positive, and produce image size is 320 * 480, if portrait way, cameras or like 320 * 480, and then corresponding into a 480 * 320 within the screen, will obviously than, then according to the rules of the vertical and horizontal screen, produce the left 90 degrees. In order to further confirm my speculation on the cause of the ratio loss, the SurfaceView loaded in my camera was adjusted to 320*213, with a ratio of approximately (320:213)*1.5=(480:320). The imaging results were left leaning as expected, but there was no ratio loss, which confirmed my idea.

In conclusion, it can be seen that the left-leaning is caused by camera mapping, while the loss-ratio is caused by pixel scale mapping.

The solution

There is no good solution, can only force landscape, record code added

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

There is no good solution, can only force landscape, record code added
android:screenOrientation="landscape"

2. Incorrect images taken, such as green screen, red and green alternate with each other, overlapping, etc

Cause analysis,

Some mobile phones do not support parameter. setPictureSize(width,height) and ES61en. setPreviewSize(width,height) methods. It is recommended not to set these two methods for compatibility.

Attached: Complete sample code:

main. xml layout file:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:orientation="vertical" >
 <LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical"
  android:layout_weight="2">
  <SurfaceView
   android:id="@+id/surfaceView"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent" />
 </LinearLayout>
 <LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:orientation="horizontal"
  android:layout_weight="1"
  android:background="@android:color/white"
  android:layout_gravity="center">
  <ImageButton android:layout_width="fill_parent"
   android:layout_height="80dip"
   android:id="@+id/btnTakePicture"
   android:layout_gravity="center"
   android:textSize="30dip"
   android:layout_weight="1"
   android:src="@drawable/btn_take_pic"/>
  <ImageButton android:layout_width="fill_parent"
   android:layout_height="80dip"
   android:id="@+id/btnAutoFocus"
   android:layout_gravity="center"
   android:textSize="30dip"
   android:layout_weight="1"
   android:src="@drawable/btn_auto_focus"/>
 </LinearLayout>
</LinearLayout>

2. MainActivity Photography core code:


package cn.itcast.takepicture;
import java.io.File;
import java.io.FileOutputStream;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.PixelFormat;
import android.graphics.Bitmap.CompressFormat;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.os.Bundle;
import android.os.Environment;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.SurfaceHolder.Callback;
import android.widget.ImageButton;
public class MainActivity extends Activity {
 private ImageButton btnTakePicture = null;
 private ImageButton btnAutoFocus = null;
 private Camera camera = null;
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  //  Set window title 
  requestWindowFeature(Window.FEATURE_NO_TITLE);
  //  landscape 
  setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
  //  Full screen 
  getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
  WindowManager.LayoutParams.FLAG_FULLSCREEN);
  //  When this window is visible to the user, keep the device on and the brightness unchanged. 
  getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
  setContentView(R.layout.main);
  SurfaceView surfaceView = (SurfaceView) this
    .findViewById(R.id.surfaceView);
  surfaceView.getHolder()
    .setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
  surfaceView.getHolder().setFixedSize(320, 240); //  Set resolution 
  surfaceView.getHolder().addCallback(new SurfaceCallback());
  btnTakePicture = (ImageButton) findViewById(R.id.btnTakePicture);
  btnAutoFocus = (ImageButton) findViewById(R.id.btnAutoFocus);
  btnTakePicture.setOnClickListener(onClickListener);
  btnAutoFocus.setOnClickListener(onClickListener);
 }
 private final View.OnClickListener onClickListener = new View.OnClickListener() {
  @Override
  public void onClick(View v) {
   if (v == btnTakePicture) {
    if (camera != null)
     camera.takePicture(null, null, new TakePictureCallback()); //  Taking pictures 
   } else if (v == btnAutoFocus) {
    if (camera != null)
     camera.autoFocus(null); //  af 
   }
  }
 };
 private final class SurfaceCallback implements Callback {
  private boolean preview; //  Are you previewing 
  @Override
  public void surfaceChanged(SurfaceHolder holder, int format, int width,
    int height) {
  }
  @Override
  public void surfaceCreated(SurfaceHolder holder) {
   try {
    camera = Camera.open();
    Camera.Parameters parameters = camera.getParameters();
    parameters.setPreviewFrameRate(5); // In a second 5 frame 
    parameters.setPictureFormat(PixelFormat.JPEG);// Set the output format of the photo 
    parameters.set("jpeg-quality", 85);// Picture quality 
    camera.setParameters(parameters);
    camera.setPreviewDisplay(holder);
    camera.startPreview();
    preview = true;
   } catch (Exception e) {
    e.printStackTrace();
   }
  }
  @Override
  public void surfaceDestroyed(SurfaceHolder holder) {
   if (camera != null) {
    if (preview) {
     camera.stopPreview();
     preview = false;
    }
    camera.release();
    camera = null; //  Remember to release 
   }
  }
 }
 private final class TakePictureCallback implements PictureCallback {
  public void onPictureTaken(byte[] data, Camera camera) {
   Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
   Matrix matrix=new Matrix();
   // Set the zoom 
   matrix.postScale(0.5f, 0.5f);
   bitmap=Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
   File file = new File(Environment.getExternalStorageDirectory(),
     System.currentTimeMillis() + ".jpg");
   try {
    FileOutputStream outStream = new FileOutputStream(file);
    bitmap.compress(CompressFormat.JPEG, 100, outStream);
    outStream.close();
    camera.startPreview();
   } catch (Exception e) {
    e.printStackTrace();
   }
  }
 }
}

Manifest file:


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="cn.itcast.takepicture"
 android:versionCode="1"
 android:versionName="1.0" >
 <application
  android:icon="@drawable/icon"
  android:label="@string/app_name" >
  <activity
   android:label="@string/app_name"
   android:name=".MainActivity" >
   <intent-filter >
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
   </intent-filter>
  </activity>
 </application>
 <uses-sdk android:minSdkVersion="7" />
 <uses-permission android:name="android.permission.CAMERA" />
 <!--  in SDCard Create and delete file permissions in  -->
 <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
 <!--  to SDCard Write data permission  -->
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>

I hope this article has been helpful for your Android programming.


Related articles: