Java 安卓:我需要一个圆形相机预览。如何实现?
我发现自己需要帮助 我正在尝试开发这个简单的拍照应用程序(哇,这是原创的!)。一切都很好。我唯一需要的是有一个圆形相机预览 我把我的camerapreview类(扩展了surfaceview)放在一个框架布局中,这基本上就是我的相机预览。大家都知道,它是长方形的。因为我有更大的应用程序计划,我需要相机预览是圆形的(例如,有人可以给某人的脸拍照,我可以在周围画一些画…) 现在,我不知道如何继续。我尝试了不同的方法,用xml创建了一个形状,并将其设置为框架布局的背景,但这根本不起作用 在谷歌上花了几个小时寻找解决方案后,我决定放弃,来到这里Java 安卓:我需要一个圆形相机预览。如何实现?,java,android,xml,camera,Java,Android,Xml,Camera,我发现自己需要帮助 我正在尝试开发这个简单的拍照应用程序(哇,这是原创的!)。一切都很好。我唯一需要的是有一个圆形相机预览 我把我的camerapreview类(扩展了surfaceview)放在一个框架布局中,这基本上就是我的相机预览。大家都知道,它是长方形的。因为我有更大的应用程序计划,我需要相机预览是圆形的(例如,有人可以给某人的脸拍照,我可以在周围画一些画…) 现在,我不知道如何继续。我尝试了不同的方法,用xml创建了一个形状,并将其设置为框架布局的背景,但这根本不起作用 在谷歌上花了几
所以,如果有人知道什么,请告诉我们:)我希望我说得足够清楚,如果需要,请不要犹豫要求澄清。您可以在相机预览上覆盖
图像视图。将SurfaceView
和ImageView
放在FrameLayout
中,两者都match\u parent
且图像必须位于顶部。
设置为中间有透明圆的黑色图像。简单方式:
1) 未为priview设置曲面
2) 捕获原始数据
3) 转换为位图并生成
4) 显示(例如在imegeview上)
仅用于示例:
public class CameraRoundPriview extends ImageView {
private Camera camera;
private Bitmap bitmap = null;
private int mPreviewWidth, mPreviewHeight;
boolean mCameraOn = false;
public CameraRoundPriview(Context context, AttributeSet attrs) {
super(context, attrs);
}
private Bitmap getclip() {
//clip
Bitmap output = Bitmap.createBitmap(bitmap.getHeight(),
bitmap.getHeight(),
Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getHeight(),
bitmap.getHeight());
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
canvas.drawCircle(bitmap.getHeight() / 2,
bitmap.getHeight() / 2,
bitmap.getHeight() / 2, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
//rotate
android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
android.hardware.Camera.getCameraInfo(getCameraID(), info);
int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
int degrees = 0;
switch (rotation) {
case Surface.ROTATION_0:
degrees = 0;
break;
case Surface.ROTATION_90:
degrees = 90;
break;
case Surface.ROTATION_180:
degrees = 180;
break;
case Surface.ROTATION_270:
degrees = 270;
break;
}
int result = degrees;
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.LOLLIPOP){
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360; // compensate the mirror
} else { // back-facing
result = (info.orientation - degrees + 360) % 360;
}
}
Matrix matrix = new Matrix();
matrix.postRotate(result);
Bitmap scaledBitmap = Bitmap.createScaledBitmap(output,output.getWidth(),output.getHeight(),true);
Bitmap rotatedBitmap = Bitmap.createBitmap(scaledBitmap , 0, 0, scaledBitmap.getWidth(), scaledBitmap .getHeight(), matrix, true);
return rotatedBitmap;
}
private void showImage(){
if ((bitmap != null)){
this.setImageBitmap(getclip());
}
}
public boolean onClickCamera(){
if (mCameraOn){
mCameraOn = false;
cameraStop();
}else{
mCameraOn = true;
cameraStart();
}
return mCameraOn;
}
private void cameraStop(){
if (camera == null) return;
camera.setPreviewCallback(null);
camera.release();
camera = null;
}
private int getCameraID(){
// specify camera id
return 0;
}
private void cameraStart(){
Camera camera = Camera.open(getCameraID());
final Camera.Size previewSize = camera.getParameters().getPreviewSize();
mPreviewWidth = previewSize.width;
mPreviewHeight = previewSize.height;
try {
camera.setPreviewCallback(new PreviewCallback() {
public void onPreviewFrame(byte[] data, Camera camera_call) {
ByteArrayOutputStream outstr = new ByteArrayOutputStream();
Rect rect = new Rect(0, 0, mPreviewWidth, mPreviewHeight);
YuvImage yuvimage=new YuvImage(data,ImageFormat.NV21,mPreviewWidth,mPreviewHeight,null);
yuvimage.compressToJpeg(rect, 50, outstr);
bitmap = BitmapFactory.decodeByteArray(outstr.toByteArray(), 0, outstr.size());
showImage();
camera_call.addCallbackBuffer(data);
}
});
} catch (Exception e) {}
camera.startPreview();
this.camera=camera;
}
}
公共类CameraRoundPriview扩展了ImageView{
私人摄像机;
私有位图=空;
私人内部审查宽度,审查宽度;
布尔值mCameraOn=false;
公共CameraRoundPriview(上下文、属性集属性){
超级(上下文,attrs);
}
私有位图getclip(){
//夹子
位图输出=Bitmap.createBitmap(Bitmap.getHeight(),
bitmap.getHeight(),
配置ARGB_8888);
画布=新画布(输出);
最终int颜色=0xFF4242;
最终油漆=新油漆();
final Rect Rect=new Rect(0,0,bitmap.getHeight(),
bitmap.getHeight());
paint.setAntiAlias(真);
drawARGB(0,0,0,0);
canvas.drawCircle(位图.getHeight()/2,
bitmap.getHeight()/2,
bitmap.getHeight()/2,绘制);
setXfermode(新的PorterDuffXfermode(Mode.SRC_IN));
绘制位图(位图、矩形、矩形、绘制);
//轮换
android.hardware.Camera.CameraInfo info=新的android.hardware.Camera.CameraInfo();
android.hardware.Camera.getCameraInfo(getCameraID(),info);
int rotation=activity.getWindowManager().getDefaultDisplay().getRotation();
整数度=0;
开关(旋转){
外壳表面旋转0:
度=0;
打破
外壳表面旋转90度:
度=90度;
打破
外壳表面旋转180度:
度=180度;
打破
外壳表面。旋转_270:
度=270度;
打破
}
int结果=度;
if(android.os.Build.VERSION.SDK_INT
如下所示:
这是针对camerax或camera2 API的
注意:背景必须透明
您必须具有如下所示的xml文件
<com.RoundedCameraPreview
android:id="@+id/viewFinder"
android:background="#00000000"
app:scaleType="fillCenter"
android:layout_width="match_parent"
android:layout_height="match_parent" />
这确实有效,但是我还需要做更多的事情,我需要一些其他的解决方案。由于活动的背景颜色将根据用户的选择而改变,因此图像的矩形将保持白色,因此这不符合我的目的。也许我应该
<declare-styleable name="PreviewView">
<attr name="isRound" format="boolean" />
</declare-styleable>
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.util.AttributeSet;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.camera.view.PreviewView;
public class RoundedCameraPreview extends PreviewView {
Path clipPath;
boolean isRound;
public RoundedCameraPreview(@NonNull Context context) {
super(context);
}
public RoundedCameraPreview(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public RoundedCameraPreview(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray a = context.getTheme().obtainStyledAttributes(
attrs,
R.styleable.PreviewView,
0, 0);
try {
isRound = a.getBoolean(R.styleable.PreviewView_isRound, true);
} finally {
a.recycle();
}
}
public boolean isRound() {
return isRound;
}
public void setIsRound(boolean isRound) {
this.isRound = isRound;
invalidate();
requestLayout();
}
public RoundedCameraPreview(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
protected void dispatchDraw(Canvas canvas) {
if (isRound) {
clipPath = new Path();
//TODO: define the circle you actually want
clipPath.addCircle(canvas.getWidth() / 2, canvas.getWidth() / 2, canvas.getWidth() / 2, Path.Direction.CW);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
canvas.clipPath(clipPath);
canvas.drawPath(clipPath, paint);
}
super.dispatchDraw(canvas);
}
}