android 中android:id="@android:id/list"与android:id="@+id/picture"

博客介绍了资源id相关内容,提到新增资源id使用@+id,引用现有的资源id使用@android:id,属于移动开发领域信息技术知识。
@+id 新增一个资源id
@android:id,引用现有的资源id
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/show_pictures" android:layout_width="match_parent" android:layout_height="match_parent"/> </RelativeLayout> <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <androidx.viewpager.widget.ViewPager android:id="@+id/change_page" android:layout_width="wrap_content" android:layout_height="wrap_content"> </androidx.viewpager.widget.ViewPager> </RelativeLayout> <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextureView android:id="@+id/textureView" android:layout_width="match_parent" android:layout_height="match_parent" /> <ImageButton android:id="@+id/recording" android:layout_width="50dp" android:layout_height="50dp" android:src="@drawable/ic_launcher_background" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="50dp" /> <ImageView android:id="@+id/image_show" android:layout_width="50dp" android:layout_height="50dp" android:layout_alignParentBottom="true" android:layout_marginBottom="50dp" android:layout_marginStart="50dp" /> <ImageButton android:id="@+id/change" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_marginEnd="50dp" android:layout_marginBottom="50dp" android:layout_alignParentEnd="true" /> <Chronometer android:id="@+id/timer" android:textColor="#f00" android:layout_width="match_parent" android:layout_height="wrap_content" android:format="%s" android:gravity="center" android:textSize="40sp" /> </RelativeLayout> <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="pemission" /> <TextureView android:id="@+id/textureView" android:layout_width="match_parent" android:layout_height="match_parent" /> <ImageButton android:id="@+id/takePicture" android:layout_width="50dp" android:layout_height="50dp" android:src="@drawable/ic_launcher_foreground" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="50dp" /> <ImageView android:id="@+id/image_show" android:layout_width="50dp" android:layout_height="50dp" android:layout_alignParentBottom="true" android:layout_marginLeft="50dp" android:layout_marginBottom="50dp" /> <ImageButton android:id="@+id/change" android:layout_width="30dp" android:layout_height="30dp" android:layout_alignParentRight="true" android:layout_alignParentBottom="true" android:layout_marginRight="50dp" android:layout_marginBottom="50dp" android:src="@drawable/ic_launcher_foreground" /> </RelativeLayout> 可以分析
最新发布
11-27
<PreferenceGroup xmlns:camera="http://schemas.android.com/apk/res/com.mediatek.camera0" camera:title="@string/pref_camera_settings_category"> <IconListPreference camera:key="pref_camera_flashmode_key" camera:defaultValue="@string/pref_camera_flashmode_default" camera:title="@string/pref_camera_flashmode_title" camera:icons="@array/camera_flashmode_icons" camera:entries="@array/pref_camera_flashmode_entries" camera:entryValues="@array/pref_camera_flashmode_entryvalues" /> <IconListPreference camera:key="pref_camera_exposure_key" camera:defaultValue="@string/pref_camera_exposure_default" camera:title="@string/pref_exposure_title" camera:icons="@array/camera_exposure_icons" camera:entries="@array/pref_camera_exposure_entries" camera:entryValues="@array/pref_camera_exposure_entryvalues" /> <IconListPreference camera:key="pref_camera_scenemode_key" camera:defaultValue="@string/pref_camera_scenemode_default" camera:title="@string/pref_camera_scenemode_title" camera:icons="@array/camera_scenemode_icons" camera:entries="@array/pref_camera_scenemode_entries" camera:entryValues="@array/pref_camera_scenemode_entryvalues" /> <IconListPreference camera:key="pref_camera_whitebalance_key" camera:defaultValue="@string/pref_camera_whitebalance_default" camera:title="@string/pref_camera_whitebalance_title" camera:icons="@array/whitebalance_icons" camera:entries="@array/pref_camera_whitebalance_entries" camera:entryValues="@array/pref_camera_whitebalance_entryvalues" /> <RecordLocationPreference camera:key="pref_camera_recordlocation_key" camera:defaultValue="@string/pref_camera_recordlocation_default" camera:title="@string/pref_camera_record_location_title" camera:entries="@array/pref_camera_recordlocation_entries" camera:entryValues="@array/pref_camera_recordlocation_entryvalues" /> <!-- camera:icons="@array/camera_recordlocation_icons" --> <!-- add by yanglh start --> <ListPreference camera:key="pref_camera_picturesize_key_htf" camera:defaultValue="@string/pref_camera_iso_default" camera:entries="@array/pref_camera_htfsize_entries" camera:entryValues="@array/pref_camera_htfsize_entryvalues" camera:title="@string/pref_camera_picturesize_title" /> <!-- add by yanglh end --> <ListPreference camera:key="pref_camera_picturesize_key" camera:title="@string/pref_camera_picturesize_title"/> <IconListPreference camera:key="pref_camera_id_key" camera:defaultValue="@string/pref_camera_id_default" camera:title="@string/pref_camera_id_title" camera:icons="@array/camera_id_icons" camera:entries="@array/camera_id_entries" /> <!-- ISO --> <ListPreference camera:key="pref_camera_iso_key" camera:defaultValue="@string/pref_camera_iso_default" camera:title="@string/pref_camera_iso_title" camera:entries="@array/pref_camera_iso_entries" camera:entryValues="@array/pref_camera_iso_entryvalues"/> <IconListPreference camera:key="perf_camera_ais_key" camera:defaultValue="@string/pref_camera_ais_default" camera:title="@string/pref_camera_ais_title" camera:entries="@array/pref_camera_ais_entries" camera:entryValues="@array/pref_camera_ais_entryvalues"/> <!-- Image adjustment --> <!-- Edge --> <ListPreference camera:key="pref_camera_edge_key" camera:defaultValue="@string/pref_camera_edge_default" camera:title="@string/pref_camera_edge_title" camera:entries="@array/pref_camera_dip_entries" camera:entryValues="@array/pref_camera_dip_entryvalues" /> <!-- Hue --> <ListPreference camera:key="pref_camera_hue_key" camera:defaultValue="@string/pref_camera_hue_default" camera:title="@string/pref_camera_hue_title" camera:entries="@array/pref_camera_dip_entries" camera:entryValues="@array/pref_camera_dip_entryvalues" /> <!-- Saturation --> <ListPreference camera:key="pref_camera_saturation_key" camera:defaultValue="@string/pref_camera_saturation_default" camera:title="@string/pref_camera_saturation_title" camera:entries="@array/pref_camera_dip_entries" camera:entryValues="@array/pref_camera_dip_entryvalues" /> <!-- Brightness --> <ListPreference camera:key="pref_camera_brightness_key" camera:defaultValue="@string/pref_camera_brightness_default" camera:title="@string/pref_camera_brightness_title" camera:entries="@array/pref_camera_dip_entries" camera:entryValues="@array/pref_camera_dip_entryvalues" /> <!-- Contrast --> <ListPreference camera:key="pref_camera_contrast_key" camera:defaultValue="@string/pref_camera_contrast_default" camera:title="@string/pref_camera_contrast_title" camera:entries="@array/pref_camera_dip_entries" camera:entryValues="@array/pref_camera_dip_entryvalues" /> <!-- Effects --> <IconListPreference camera:key="pref_camera_coloreffect_key" camera:defaultValue="@string/pref_camera_coloreffect_default" camera:title="@string/pref_camera_coloreffect_title" camera:icons="@array/camera_coloreffect_icons" camera:entries="@array/pref_camera_coloreffect_entries" camera:entryValues="@array/pref_camera_coloreffect_entryvalues" /> <!-- Self timer setting --> <IconListPreference camera:key="pref_camera_self_timer_key" camera:defaultValue="@string/pref_camera_selftimer_default" camera:title="@string/pref_camera_selftimer_title" camera:icons="@array/camera_selftimer_icons" camera:entries="@array/pref_camera_selftimer_entries" camera:entryValues="@array/pref_camera_selftimer_entryvalues"/> <!-- anti-flicker --> <ListPreference camera:key="pref_camera_antibanding_key" camera:defaultValue="@array/pref_camera_antibanding_default_array" camera:title="@string/pref_camera_antibanding_title" camera:entries="@array/pref_camera_antibanding_entries" camera:entryValues="@array/pref_camera_antibanding_entryvalues" /> <ListPreference camera:key="pref_camera_zsd_key" camera:defaultValue="@string/pref_camera_zsd_default" camera:title="@string/pref_camera_zsd_title" camera:entries="@array/pref_camera_zsd_entries" camera:entryValues="@array/pref_camera_zsd_entryvalues" /> <ListPreference camera:key="pref_camera_shot_number" camera:defaultValue="@string/pref_camera_continuous_number_default" camera:title="@string/pref_camera_continuous_number_title" camera:entries="@array/pref_camera_continuous_number_entries" camera:entryValues="@array/pref_camera_continuous_number_entryvalues" /> <ListPreference camera:key="pref_camera_Volume_shortcut_keys" camera:defaultValue="@string/pref_camera_Volume_shortcut_key_default" camera:title="@string/pref_camera_Volume_shortcut_key_title" camera:entries="@array/pref_camera_Volume_shortcut_key_entries" camera:entryValues="@array/pref_camera_Volume_shortcut_key_entryvalues" /> <!-- video setting start --> <IconListPreference camera:key="pref_video_quality_key" camera:title="@string/pref_video_quality_title" camera:entries="@array/pref_video_quality_entries" camera:entryValues="@array/pref_video_quality_entryvalues"/> <IconListPreference camera:key="pref_slow_motion_video_quality_key" camera:title="@string/pref_slow_motion_video_quality_title" camera:entries="@array/pref_slow_motion_video_quality_entries" camera:entryValues="@array/pref_slow_motion_video_quality_entryvalues"/> <IconListPreference camera:key="pref_refocus_video_quality_key" camera:title="@string/pref_refocus_video_quality_title" camera:entries="@array/pref_refocus_video_quality_entries" camera:entryValues="@array/pref_refocus_video_quality_entryvalues"/> <ListPreference camera:key="pref_camera_recordaudio_key" camera:defaultValue="@string/pref_camera_recordaudio_default" camera:title="@string/pref_camera_recordaudio_title" camera:entries="@array/pref_camera_recordaudio_entries" camera:entryValues="@array/pref_camera_recordaudio_entryvalues"/> <ListPreference camera:key="pref_camera_video_hd_recording_key" camera:defaultValue="@string/pref_video_hd_recording_default" camera:title="@string/pref_camera_video_hd_recording_title" camera:entries="@array/pref_camera_video_hd_recording_entries" camera:entryValues="@array/pref_camera_video_hd_recording_entryvalues" /> <!-- video setting end --> <ListPreference camera:key="pref_camera_image_properties_key" camera:defaultValue="@string/pref_camera_image_properties_default" camera:title="@string/pref_camera_image_properties_title" camera:entries="@array/pref_camera_image_properties_entries" camera:entryValues="@array/pref_camera_image_properties_entryvalues" /> <ListPreference camera:defaultValue="@string/pref_dual_camera_default" camera:entries="@array/pref_dual_camera_entries" camera:entryValues="@array/pref_dual_camera_entryvalues" camera:key="pref_dual_camera_key" camera:title="@string/pref_dual_camera_switch" /> <ListPreference camera:defaultValue="@string/pref_fast_af_default" camera:entries="@array/pref_dual_camera_dip_entries" camera:entryValues="@array/pref_dual_camera_dip_entryvalues" camera:key="pref_fast_af_key" camera:title="@string/pref_fast_af_title" /> <ListPreference camera:defaultValue="@string/pref_distance_default" camera:entries="@array/pref_dual_camera_dip_entries" camera:entryValues="@array/pref_dual_camera_dip_entryvalues" camera:key="pref_distance_key" camera:title="@string/pref_distance_info_title" /> <IconListPreference camera:key="pref_camera_picturesize_ratio_key" camera:title="@string/pref_camera_picturesize_ratio_title" camera:entries="@array/pref_camera_picturesize_ratio_entries" camera:entryValues="@array/pref_camera_picturesize_ratio_entryvalues" camera:icons="@array/pref_htf_preview_icons"/> <IconListPreference camera:key="pref_voice_key" camera:defaultValue="@string/pref_voice_default" camera:title="@string/pref_voice_capture" camera:entries="@array/pref_voice_entries" camera:entryValues="@array/pref_voice_entryvalues" /> <IconListPreference camera:key="pref_slow_motion_key" camera:defaultValue="@string/pref_slow_motion_default" camera:title="@string/pref_slow_motion_title" camera:icons="@array/slow_motion_icons" camera:entries="@array/pref_slow_motion_entries" camera:entryValues="@array/pref_slow_motion_entryvalues" /> <ListPreference camera:key="pref_camera_facebeauty_properties_key" camera:defaultValue="@string/pref_camera_facebeauty_properties_default" camera:title="@string/pref_camera_facebeauty_properties_title" camera:entries="@array/pref_camera_facebeauty_properties_entries" camera:entryValues="@array/pref_camera_facebeauty_properties_entryvalues" /> <!-- facebeauty adjustment --> <!-- Smooth --> <IconListPreference camera:key="pref_facebeauty_smooth_key" camera:defaultValue="@string/pref_facebeauty_smooth_default" camera:title="@string/pref_facebeauty_smooth_title" camera:icons="@array/facebeauty_smooth_icons" camera:entries="@array/pref_camera_dip_entries" camera:entryValues="@array/pref_camera_facebeauty_turning_entryvalues" /> <!-- Skin color --> <IconListPreference camera:key="pref_facebeauty_skin_color_key" camera:defaultValue="@string/pref_facebeauty_skin_color_default" camera:title="@string/pref_facebeauty_skin_color_title" camera:icons="@array/facebeauty_skin_color_icons" camera:entries="@array/pref_camera_dip_entries" camera:entryValues="@array/pref_camera_facebeauty_turning_entryvalues" /> <!-- Sharp --> <IconListPreference camera:key="pref_facebeauty_sharp_key" camera:defaultValue="@string/pref_facebeauty_sharp_default" camera:title="@string/pref_facebeauty_sharp_title" camera:icons="@array/facebeauty_sharp_icons" camera:entries="@array/pref_camera_dip_entries" camera:entryValues="@array/pref_camera_facebeauty_turning_entryvalues" /> <ListPreference camera:key="pref_video_eis_key" camera:defaultValue="@string/pref_camera_eis_default" camera:title="@string/pref_camera_eis_title" camera:entries="@array/pref_camera_eis_entries" camera:entryValues="@array/pref_camera_eis_entryvalues"/> <ListPreference camera:key="pref_video_3dnr_key" camera:defaultValue="@string/pref_camera_3dnr_default" camera:title="@string/pref_camera_3dnr_title" camera:entries="@array/pref_camera_3dnr_entries" camera:entryValues="@array/pref_camera_3dnr_entryvalues"/> <ListPreference camera:key="pref_face_detect_key" camera:defaultValue="@string/pref_camera_face_detect_default" camera:title="@string/pref_camera_face_detect_title" camera:entries="@array/pref_camera_face_detect_entries" camera:entryValues="@array/pref_camera_face_detect_entryvalues"/> <ListPreference camera:key="pref_face_beauty_multi_mode_key" camera:title="@string/pref_face_beauty_mode_title" camera:entries="@array/pref_camera_multi_face_beauty_entries" camera:entryValues="@array/pref_camera_multi_face_beauty_entryvalues"/> <ListPreference camera:key="pref_af_mode_key" camera:defaultValue="@string/pref_af_mode_default" camera:title="@string/pref_af_mode_title" camera:entries="@array/pref_af_entries" camera:entryValues="@array/pref_af_entryvalues"/> <IconListPreference camera:key="pref_hdr_key" camera:defaultValue="@string/pref_camera_hdr_default" camera:title="@string/pref_camera_hdr_title" camera:icons="@array/pref_hdr_icons" camera:entries="@array/pref_camera_hdr_entries" camera:entryValues="@array/pref_camera_hdr_entryvalues"/> <ListPreference camera:key="pref_asd_key" camera:defaultValue="@string/pref_asd_default" camera:title="@string/pref_asd_title" camera:entries="@array/pref_asd_entries" camera:entryValues="@array/pref_asd_entryvalues"/> <IconListPreference camera:key="pref_dng_key" camera:defaultValue="@string/pref_dng_default" camera:title="@string/pref_dng_title" camera:icons="@array/dng_icons" camera:entries="@array/pref_dng_entries" camera:entryValues="@array/pref_dng_entryvalues"/> <!--add by yanglh start --> <IconListPreference camera:key="pref_htf_settings_key" camera:defaultValue="@string/pref_camera_hdr_default" camera:title="@string/pref_camera_settings_category" camera:icons="@array/pref_htf_settings_icons" camera:entries="@array/pref_htf_settings_entries" camera:entryValues="@array/pref_htf_settings_entryvalues"/> <!-- <IconListPreference camera:key="pref_camera_picturesize_htficon_key" camera:title="@string/pref_camera_picturesize_ratio_title" camera:entries="@array/pref_camera_picturesize_htf_entries" camera:entryValues="@array/pref_camera_picturesize_htf_entryvalues" camera:defaultValue="@string/pref_camera_hdr_default" camera:icons="@array/pref_htf_preview_icons"/> --> <IconListPreference camera:key="pref_htf_color_key" camera:defaultValue="@string/pref_camera_hdr_default" camera:title="@string/pref_camera_settings_category" camera:icons="@array/pref_htf_color_icons" camera:entries="@array/pref_htf_color_entries" camera:entryValues="@array/pref_htf_color_entryvalues"/> <!--add by yanglh end--> </PreferenceGroup> 当前界面应该如何修改,使其可以上下滑动
11-05
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto"> <androidx.appcompat.widget.LinearLayoutCompat android:layout_width="match_parent" android:orientation="vertical" android:layout_height="wrap_content"> <androidx.appcompat.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" app:title="" app:navigationIcon="@drawable/back"> </androidx.appcompat.widget.Toolbar> <ImageView android:layout_width="match_parent" android:layout_height="180dp" android:src="@mipmap/picture_8" android:scaleType="centerCrop"/> <androidx.appcompat.widget.LinearLayoutCompat android:layout_width="match_parent" android:layout_margin="10dp" android:orientation="vertical" android:layout_height="wrap_content"> </androidx.appcompat.widget.LinearLayoutCompat> <TextView android:layout_marginTop="5dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="泰山,又名岱山、岱宗、岱岳、东岳、泰岳,为五岳之一,有“五岳之首”、“五岳独尊”、“天下第一山”之称,被中外学者称为“中国的奥林匹斯山”,位于山东省中部,隶属于泰安市,绵亘于泰安、济南、淄博三市之间,总面积25000公顷,主峰玉皇顶海拔约1545米"/> </androidx.appcompat.widget.LinearLayoutCompat> </RelativeLayout>
06-24
package com.android.example.cameraappxjava; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.ActivityCompat; import android.content.pm.PackageManager; import android.graphics.ImageFormat; import android.graphics.SurfaceTexture; import android.hardware.camera2.CameraAccessException; import android.hardware.camera2.CameraCaptureSession; import android.hardware.camera2.CameraCharacteristics; import android.hardware.camera2.CameraDevice; import android.hardware.camera2.CameraManager; import android.hardware.camera2.CaptureRequest; import android.hardware.camera2.params.StreamConfigurationMap; import android.media.Image; import android.media.ImageReader; import android.os.Bundle; import android.util.Log; import android.util.Size; import android.view.Surface; import android.view.TextureView; import android.view.View; import android.widget.Toast; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.nio.ByteBuffer; import java.util.Arrays; import java.util.Collections; import java.util.List; import android.Manifest; import android.content.Context; public class MainActivity extends AppCompatActivity { private CameraManager cameraManager; private String cameraId; private Size photoSize; private TextureView textureView; private CameraDevice cameraDevice; private CameraCaptureSession captureSession; private ImageReader imageReader; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); CameraUtils.init(this); initCamera(); initViews(); setupTextureListener(); } private void initCamera() { List<Size> outputSizes; try { // 1. 获取CameraUtils单例 CameraUtils cameraUtils = CameraUtils.getInstance(); // 2. 获取相机管理服务 cameraManager = cameraUtils.getCameraManager(); if (cameraManager == null) { throw new IllegalStateException("CameraManager未初始化"); } // 3. 获取后置摄像头ID cameraId = cameraUtils.getBackCameraId(); if (cameraId == null) { throw new IllegalStateException("未找到后置摄像头"); } // 4. 获取相机支持的输出尺寸(用于预览) outputSizes = cameraUtils.getCameraOutputSizes(cameraId, SurfaceTexture.class); if (outputSizes == null || outputSizes.isEmpty()) { throw new IllegalStateException("未获取到有效输出尺寸"); } // 5. 选择最佳预览尺寸(这里选择最大尺寸,实际应适配屏幕比例) photoSize = outputSizes.get(0); for (Size size : outputSizes) { if (size.getWidth() > photoSize.getWidth()) { photoSize = size; } } Log.d("CameraInit", "相机初始化完成 | 分辨率: " + photoSize.getWidth() + "x" + photoSize.getHeight()); } catch (CameraAccessException e) { Log.e("CameraError", "相机访问异常", e); Toast.makeText(this, "相机访问失败: " + e.getMessage(), Toast.LENGTH_LONG).show(); } catch (IllegalStateException e) { Log.e("CameraError", "相机状态异常", e); Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show(); } } private void initViews() { textureView = findViewById(R.id.texture_view); } private void setupTextureListener() { textureView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() { @Override public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) { openCamera(); } @Override public void onSurfaceTextureSizeChanged(@NonNull SurfaceTexture surface, int width, int height) { } @Override public boolean onSurfaceTextureDestroyed(@NonNull SurfaceTexture surface) { return false; } @Override public void onSurfaceTextureUpdated(@NonNull SurfaceTexture surface) { } // 其他回调方法省略... }); } private void openCamera() { try { if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) { cameraManager.openCamera(cameraId, new CameraDevice.StateCallback() { @Override public void onOpened(@NonNull CameraDevice camera) { cameraDevice = camera; createPreviewSession(); } @Override public void onDisconnected(@NonNull CameraDevice camera) { } @Override public void onError(@NonNull CameraDevice camera, int error) { } // 其他回调方法省略... }, null); } } catch (CameraAccessException e) { e.printStackTrace(); } } private void createPreviewSession() { try { SurfaceTexture texture = textureView.getSurfaceTexture(); texture.setDefaultBufferSize(photoSize.getWidth(), photoSize.getHeight()); Surface previewSurface = new Surface(texture); CaptureRequest.Builder previewBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW); previewBuilder.addTarget(previewSurface); cameraDevice.createCaptureSession(Collections.singletonList(previewSurface), new CameraCaptureSession.StateCallback() { @Override public void onConfigured(@NonNull CameraCaptureSession session) { captureSession = session; try { session.setRepeatingRequest(previewBuilder.build(), null, null); } catch (CameraAccessException e) { e.printStackTrace(); } } @Override public void onConfigureFailed(@NonNull CameraCaptureSession session) { } // 其他回调方法省略... }, null); } catch (CameraAccessException e) { e.printStackTrace(); } } public void capturePhoto(View view) { if (cameraDevice == null) return; try { imageReader = ImageReader.newInstance( photoSize.getWidth(), photoSize.getHeight(), ImageFormat.JPEG, 2); imageReader.setOnImageAvailableListener(reader -> { Image image = reader.acquireNextImage(); saveImage(image); image.close(); }, null); CaptureRequest.Builder captureBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE); captureBuilder.addTarget(imageReader.getSurface()); captureBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE); captureSession.stopRepeating(); captureSession.capture(captureBuilder.build(), null, null); } catch (CameraAccessException e) { e.printStackTrace(); } } private void saveImage(Image image) { ByteBuffer buffer = image.getPlanes()[0].getBuffer(); byte[] bytes = new byte[buffer.remaining()]; buffer.get(bytes); File file = new File(getExternalFilesDir(null), "photo_"+System.currentTimeMillis()+".jpg"); try (FileOutputStream output = new FileOutputStream(file)) { output.write(bytes); Toast.makeText(this, "Saved: " + file.getAbsolutePath(), Toast.LENGTH_SHORT).show(); } catch (IOException e) { e.printStackTrace(); } } // CameraUtils类扩展 public static class CameraUtils { private static Context appContext; private static CameraManager cameraManager; private static CameraUtils instance; public static void init(Context context) { if (appContext == null) { appContext = context.getApplicationContext(); cameraManager = (CameraManager) appContext.getSystemService(Context.CAMERA_SERVICE); } } public static CameraUtils getInstance() { if (instance == null) { instance = new CameraUtils(); } return instance; } public CameraManager getCameraManager() { return cameraManager; } public String getBackCameraId() throws CameraAccessException { for (String id : cameraManager.getCameraIdList()) { CameraCharacteristics characteristics = cameraManager.getCameraCharacteristics(id); Integer facing = characteristics.get(CameraCharacteristics.LENS_FACING); if (facing != null && facing == CameraCharacteristics.LENS_FACING_BACK) { return id; } } return null; } public List<Size> getCameraOutputSizes(String cameraId, Class klass) throws CameraAccessException { CameraCharacteristics characteristics = cameraManager.getCameraCharacteristics(cameraId); StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP); return Arrays.asList(map.getOutputSizes(klass)); } } } 为什么我运行后预览黑屏,<?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" tools:context=".MainActivity"> <androidx.camera.view.PreviewView android:id="@+id/viewFinder" android:layout_width="match_parent" android:layout_height="match_parent" /> <Button android:id="@+id/image_capture_button" android:layout_width="110dp" android:layout_height="110dp" android:layout_marginBottom="50dp" android:layout_marginEnd="50dp" android:elevation="2dp" android:text="@string/take_photo" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintEnd_toStartOf="@id/vertical_centerline" /> <Button android:id="@+id/video_capture_button" android:layout_width="110dp" android:layout_height="110dp" android:layout_marginBottom="50dp" android:layout_marginStart="50dp" android:elevation="2dp" android:text="@string/start_capture" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toEndOf="@id/vertical_centerline" /> <androidx.constraintlayout.widget.Guideline android:id="@+id/vertical_centerline" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" app:layout_constraintGuide_percent=".50" /> <TextureView android:id="@+id/texture_view" android:layout_width="match_parent" android:layout_height="match_parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
09-06
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值