在 Android 手機內的相機是可以將影像放大(Zoom In)或縮小(Zoom out),放大或縮小也是相機內的參數,祇要用 android.hardware.Camera的 API(Camera1)便可以設定相機的放大或縮小參數(Camera Zoom Parameters),筆者編寫了簡單的相機參數程式(Camera Zoom In / Zoom out Program)。
Android 相機放大縮小程式(Camera Zoom In / Zoom out Program) |
- 操作系統:Windows 7 64-bit 版本
- 開發環境:Android Studio 4.0.1 版本
- 原程式:C:\Development\Development_Android\Android_Project\DIY-Android-091-11f Camera Zoom Bugworkshop
- 程式:C:\Development\Development_Android\Android_Project\DIY-Android-091-11f Camera Zoom Bugworkshop
- 測試環境:Vivo(維沃)X5Max Android 4.4.4(KitKat)版本
Android相機放大縮小程式(Camera Zoom In / Zoom out Program)編程步驟:
- 設定 Camera 參數
- 設置 layout ScrollView 參數 - activity_camera.xml
- 設置放大縮小參數
AndroidManifest.xml 獲取 Camera 許可權:
<uses-permission android:name="android.permission.CAMERA" /> |
activity_camera_zoom.xml:
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent">
<SurfaceView android:id="@+id/activity_camera_surface" android:layout_width="match_parent" android:layout_height="match_parent" />
<Button android:id="@+id/btn_camera_zoomin" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="124dp" android:layout_marginLeft="124dp" android:layout_marginTop="200dp" android:text="Zoom In +" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" />
<Button android:id="@+id/btn_camera_zoomout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="200dp" android:text="Zoom Out -" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.511" app:layout_constraintStart_toEndOf="@+id/btn_camera_zoomin" app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout> |
CameraZoom.java:
package bugworkshop.blogspot.com.camera;
import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.util.Log;
// Camera import android.hardware.Camera;
// SurfaceView import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import android.widget.Button;
// Takephoto import android.graphics.Bitmap; import android.graphics.BitmapFactory; import java.io.ByteArrayOutputStream; import android.graphics.Matrix; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException;
public class CameraZoom extends AppCompatActivity implements SurfaceHolder.Callback{
//private static final String tag = MainActivity.class.getSimpleName(); private static final String TAG = "CameraActivity";
private Button mbtn_camera_zoomin; private Button mbtn_camera_zoomout;
// Camera private Camera mCamera;
// SurfaceView private SurfaceView mSurfaceView; private SurfaceHolder mSurfaceHolder;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_camera_zoom);
mbtn_camera_zoomin = (Button)findViewById(R.id.btn_camera_zoomin); mbtn_camera_zoomout = (Button)findViewById(R.id.btn_camera_zoomout);
mbtn_camera_zoomin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.d(TAG, "MainActivity - onClick Zoom In"); setZoom(true); } });
mbtn_camera_zoomout.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.d(TAG, "MainActivity - onClick Zoom Out"); setZoom(false); } });
init_View();
}
//@Override public void init_View() { Log.d(TAG, "CameraActivity - init_View()");
mSurfaceView = (SurfaceView)findViewById(R.id.activity_camera_surface); mSurfaceHolder = mSurfaceView.getHolder(); mSurfaceHolder.addCallback(this); mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); }
@Override public void surfaceCreated(@NonNull SurfaceHolder surfaceHolder) { mCamera = Camera.open(); if (mCamera != null) { Camera.Parameters p = mCamera.getParameters(); p.setPreviewSize(640, 480); p.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE); mCamera.setParameters(p); }
try { mCamera.setPreviewDisplay(surfaceHolder); mCamera.startPreview(); } catch (IOException e) { e.printStackTrace(); }
}
@Override public void surfaceChanged(@NonNull SurfaceHolder surfaceHolder, int i, int i1, int i2) { }
@Override public void surfaceDestroyed(@NonNull SurfaceHolder surfaceHolder) { }
// Take Photo //@Override public void setZoom(boolean isZoomIn) { Log.d(TAG, "CameraRecord - CameraZoom()");
Camera.Parameters parameters = mCamera.getParameters(); parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_MACRO); int maxZoom = parameters.getMaxZoom(); int curZoom = parameters.getZoom();
Log.d(TAG, "setZoom - MaxZoom = " + maxZoom + " CurrZoom = " + curZoom);
if (isZoomIn && curZoom < maxZoom) { //curZoom++; curZoom = curZoom+10; } else if (curZoom > 0) { //curZoom--; curZoom = curZoom-10; }
//curZoom = curZoom+10; parameters.setZoom(curZoom); mCamera.setParameters(parameters);
} } |
沒有留言:
張貼留言