// Import necessary packages
import android.hardware.Camera;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
// Define class that implements SurfaceHolder.Callback
public class CustomCamera implements SurfaceHolder.Callback {
// Declare variables
private Camera mCamera;
private SurfaceHolder mHolder;
// Constructor
public CustomCamera(SurfaceView surfaceView) {
// Get SurfaceHolder from SurfaceView
mHolder = surfaceView.getHolder();
// Set this class as the callback for SurfaceHolder events
mHolder.addCallback(this);
}
// Implement SurfaceHolder.Callback methods
@Override
public void surfaceCreated(SurfaceHolder holder) {
// Open camera and set preview display
mCamera = Camera.open();
try {
mCamera.setPreviewDisplay(holder);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
// Set camera parameters for preview size and aspect ratio
Camera.Parameters parameters = mCamera.getParameters();
Camera.Size previewSize = getOptimalPreviewSize(parameters.getSupportedPreviewSizes(), 4, 3);
parameters.setPreviewSize(previewSize.width, previewSize.height);
mCamera.setParameters(parameters);
mCamera.startPreview();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// Release camera resources
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
// Define method to get optimal preview size based on aspect ratio
private Camera.Size getOptimalPreviewSize(List<Camera.Size> sizes, int width, int height) {
final double ASPECT_TOLERANCE = 0.1;
double targetRatio = (double) width / height;
Camera.Size optimalSize = null;
double minDiff = Double.MAX_VALUE;
int targetHeight = height;
// Try to find a size that matches the target aspect ratio and has the closest height
for (Camera.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);
}
}
// If no size matches the target aspect ratio, choose the one with the closest aspect ratio
if (optimalSize == null) {
minDiff = Double.MAX_VALUE;
for (Camera.Size size : sizes) {
double ratio = (double) size.width / size.height;
if (Math.abs(ratio - targetRatio) < minDiff) {
optimalSize = size;
minDiff = Math.abs(ratio - targetRatio);
}
}
}
return optimalSize;
}
}
// To get a 4:3 aspect ratio, we need to find the optimal preview size with a width-to-height ratio of 4:3.
// We can do this by iterating through the list of supported preview sizes and finding the one with the closest height to the target height (which is 3/4 of the target width).
// If there is no size with a 4:3 aspect ratio, we choose the one with the closest aspect ratio.
// We then set the camera parameters to use this preview size.
Android camera1 支持4:3
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來澜术,“玉大人艺蝴,你說我怎么就攤上這事∧穹希” “怎么了猜敢?”我有些...
- 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)盒延。 經(jīng)常有香客問我缩擂,道長(zhǎng),這世上最難降的妖魔是什么添寺? 我笑而不...
- 正文 為了忘掉前任胯盯,我火速辦了婚禮,結(jié)果婚禮上计露,老公的妹妹穿的比我還像新娘博脑。我一直安慰自己,他們只是感情好票罐,可當(dāng)我...
- 文/花漫 我一把揭開白布叉趣。 她就那樣靜靜地躺著,像睡著了一般胶坠。 火紅的嫁衣襯著肌膚如雪君账。 梳的紋絲不亂的頭發(fā)上,一...
- 文/蒼蘭香墨 我猛地睜開眼玖翅,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起金度,我...
- 序言:老撾萬榮一對(duì)情侶失蹤应媚,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后猜极,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體中姜,經(jīng)...
- 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
- 正文 我和宋清朗相戀三年跟伏,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了丢胚。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
- 正文 年R本政府宣布,位于F島的核電站相满,受9級(jí)特大地震影響层亿,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜立美,卻給世界環(huán)境...
- 文/蒙蒙 一匿又、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧建蹄,春花似錦碌更、人聲如沸。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽睁宰。三九已至搂妻,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間京痢,已是汗流浹背焦人。 一陣腳步聲響...
- 正文 我出身青樓忽匈,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國和親矿辽。 傳聞我的和親對(duì)象是個(gè)殘疾皇子丹允,可洞房花燭夜當(dāng)晚...
推薦閱讀更多精彩內(nèi)容
- 最近公司開發(fā)的一款型號(hào)客戶要求相機(jī)拍照分辨率最大支持到3600萬像素郭厌,相機(jī)模組硬件支持的最大分辨率為400萬,驅(qū)...
- 一液走、Camera 架構(gòu) 架構(gòu)簡(jiǎn)圖: ? 根據(jù)架構(gòu)簡(jiǎn)圖可以看到碳默,實(shí)際上 Camera 的架構(gòu)與 An...
- 版權(quán)聲明:本文為博主原創(chuàng)文章贾陷,遵循CC 4.0 BY-SA[http://creativecommons.org/...
- 這篇文章主要介紹基于虹軟人臉識(shí)別SDK,適配Camera1冈爹、Camera2、CameraX欧引。文章分下面幾點(diǎn)展開频伤。 ...
- Android Camera2入門 Android Camera2入門系列1 - Camera2在textureV...