public
class
usecamera2_2activity
extends
catch
(cameraacces***ception e)
}@override
public
void
onsu***cetexturesizechanged
(@nonnull su***cetexture su***ce,
int width,
int height)
@override
public
boolean
onsu***cetexturedestroyed
(@nonnull su***cetexture su***ce)
@override
public
void
onsu***cetextureupdated
(@nonnull su***cetexture su***ce)})
;}/** * 1. 獲取所有hardware level在full以上的相機id(函式有問題??刪除)
* 2. 獲取前後相機(0:後攝像頭,1:前置攝像頭)
* */
private string[
]getfrontbackcameraid
(string[
] cameraidlist)
throws cameraacces***ception
if(characteristics.
get(cameracharacteristics.lens_facing)
== cameracharacteristics.lens_facing_back)
}// 獲取合格的camera
return frontbackid;
}@suppresslint
("missingpermission"
)private
void
opencameras
(string cameraid)
catch
(cameraacces***ception e)
}/**監聽相機開啟狀態*/
cameradevice.statecallback camerastatecallback =
newcameradevice.statecallback()
catch
(cameraacces***ception e)
}@override
public
void
ondisconnected
(@nonnull cameradevice camera)
@override
public
void
onerror
(@nonnull cameradevice camera,
int error)};
/**獲取相機支援的預覽尺寸*/
private size[
]getconfigurationmap
(string id)
catch
(cameraacces***ception e)
return null;
}/**選取支援尺寸中最相符合的項
* @param aspectratio: 短邊/長邊。
* */
private size getbestmatchsize
(size[
] supportsizes,
float aspectratio)
}// 找到尺寸正合適的直接返回
float mindiff =1;
// 比例差不可能大於1
size bestmatchsize = supportsizes[0]
;for
(size size : supportsizes)
}// 獲取比例最接近的size(允許影象變形時新增此段)
return bestmatchsize;
}/**
* 8. 『捕獲會話』(capturesession)類狀態監聽。
*/cameracapturesession.statecallback sessionstatecallback =
newcameracapturesession.statecallback()
catch
(cameraacces***ception e)
}@override
public
void
onconfigurefailed
(@nonnull cameracapturesession session)};
@override
protected
void
ondestroy()
}}
<?xml version="1.0" encoding="utf-8"?>
android:layout_width
="match_parent"
android:layout_height
="match_parent"
android:orientation
="vertical"
xmlns:android
="">
android:id
="@+id/camera_preview"
android:layout_width
="600dp"
android:layout_height
="800dp"
/>
relativelayout
>
Camera2挖坑日記 如何解決預覽畫面變形
一夢覺來三十載,休休。空為梅花白了頭 camera2是目前android相機開發最新的api,舊版本的camera已經被廢棄了。一般情況下,camera2的使用是將影象傳送到su ceview或者textureview 通過su cetexture 來預覽。使用jpeg或者raw sensor格式的...
Camera2例子鏈結
最後,在網上發現幾個使用camera api2開發的例項程式,總結一下方便後續參考 1.camera2 basic 演示如何使用camera2 api基本功能的demo。你可以從中學會如何遍歷相機裝置的所有特性,顯示預覽 以及拍照。2.camera2 video l camera是乙個開源的實驗性質...
camera (2)雙攝的作用
這兩年手機的亮點越來越少,高階智慧型機現在基本都不再去比拼雷兔兔,而開始pk拍照效能。於是乎,這兩年各家紛紛開始做起了雙camera。那問題來了,雙camera到底能做什麼?1 雙camera可以測距,所以可以做距離相關的應用 如上圖,由於雙camera通過演算法,可以判斷被攝物體的距離,所以通過此...