- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是第一次接触 Android,我的预览出现问题,顶部和底部都被空白包围,我不确定是什么原因造成的。非常感谢任何帮助。
Activity 主线:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.baltimorehackathon.MainActivity" >
<SurfaceView
android:id="@+id/camerapreview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
预览:
import java.io.IOException;
import java.util.List;
import android.content.Context;
import android.hardware.Camera;
import android.hardware.Camera.Size;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
public class Preview extends ViewGroup implements SurfaceHolder.Callback {
private final String TAG = "Preview";
SurfaceView mSurfaceView;
SurfaceHolder mHolder;
Size mPreviewSize;
List<Size> mSupportedPreviewSizes;
Camera mCamera;
private boolean cameraConfigured;
public Preview(Context context) {
super(context);
mSurfaceView = new SurfaceView(context);
addView(mSurfaceView);
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = mSurfaceView.getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void setCamera(Camera camera) {
mCamera = camera;
if (mCamera != null) {
mSupportedPreviewSizes = mCamera.getParameters().getSupportedPreviewSizes();
requestLayout();
}
}
public void switchCamera(Camera camera) {
setCamera(camera);
try {
camera.setPreviewDisplay(mHolder);
} catch (IOException exception) {
Log.e(TAG, "IOException caused by setPreviewDisplay()", exception);
}
Camera.Parameters parameters = camera.getParameters();
parameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height);
requestLayout();
camera.setParameters(parameters);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
final int width = resolveSize(getSuggestedMinimumWidth(), widthMeasureSpec);
final int height = resolveSize(getSuggestedMinimumHeight(), heightMeasureSpec);
setMeasuredDimension(width, height);
if (mSupportedPreviewSizes != null) {
mPreviewSize = getOptimalPreviewSize(mSupportedPreviewSizes, width, height);
}
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
if (changed && getChildCount() > 0) {
final View child = getChildAt(0);
final int width = r - l;
final int height = b - t;
int previewWidth = width;
int previewHeight = height;
if (mPreviewSize != null) {
previewWidth = mPreviewSize.width;
previewHeight = mPreviewSize.height;
}
// Center the child SurfaceView within the parent.
if (width * previewHeight > height * previewWidth) {
final int scaledChildWidth = previewWidth * height / previewHeight;
child.layout((width - scaledChildWidth) / 2, 0,
(width + scaledChildWidth) / 2, height);
} else {
final int scaledChildHeight = previewHeight * width / previewWidth;
child.layout(0, (height - scaledChildHeight) / 2,
width, (height + scaledChildHeight) / 2);
}
}
}
public void surfaceCreated(SurfaceHolder holder) {
try {
if (mCamera != null) {
mCamera.setPreviewDisplay(holder);
}
} catch (IOException exception) {
Log.e(TAG, "IOException caused by setPreviewDisplay()", exception);
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
if (mCamera != null) {
mCamera.stopPreview();
}
}
private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) {
final double ASPECT_TOLERANCE = 0.1;
double targetRatio = (double) w / h;
if (sizes == null) return null;
Size optimalSize = null;
double minDiff = Double.MAX_VALUE;
int targetHeight = h;
// Try to find an size match aspect ratio and size
for (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);
}
}
// Cannot find the one match the aspect ratio, ignore the requirement
if (optimalSize == null) {
minDiff = Double.MAX_VALUE;
for (Size size : sizes) {
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
}
return optimalSize;
}
private Camera.Size getBestPreviewSize(int width, int height)
{
Camera.Size result=null;
Camera.Parameters p = mCamera.getParameters();
for (Camera.Size size : p.getSupportedPreviewSizes()) {
if (size.width<=width && size.height<=height) {
if (result==null) {
result=size;
} else {
int resultArea=result.width*result.height;
int newArea=size.width*size.height;
if (newArea>resultArea) {
result=size;
}
}
}
}
return result;
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
if(mCamera != null){
if (!cameraConfigured ) {
Camera.Parameters parameters=mCamera.getParameters();
Camera.Size size = getBestPreviewSize(w,h);
if (size != null) {
parameters.setPreviewSize(size.width, size.height);
mCamera.setDisplayOrientation(90);
mCamera.setParameters(parameters);
cameraConfigured=true;
}
mCamera.startPreview();
}
}
}
}
主要 Activity :
import android.app.Activity;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
private Preview mPreview;
Camera mCamera;
int numberOfCameras;
int cameraCurrentlyLocked;
int defaultCameraId;
boolean cameraConfigured;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPreview = new Preview(this);
setContentView(mPreview);
mPreview.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mCamera.autoFocus(null);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onPause() {
super.onPause();
// Because the Camera object is a shared resource, it's very
// important to release it when the activity is paused.
if (mCamera != null) {
mPreview.setCamera(null);
mCamera.release();
mCamera = null;
}
}
@Override
protected void onResume() {
super.onResume();
// Open the default i.e. the first rear facing camera.
mCamera = Camera.open();
cameraCurrentlyLocked = defaultCameraId;
mPreview.setCamera(mCamera);
}
}
最佳答案
这是因为您的 Preview 类找到了最适合相机的宽高比,但它恰好不太适合屏幕。
您可以将背景颜色更改为其他颜色:
public Preview(Context context) {
super(context);
setBackgroundColor(Color.BLACK);
...
}
或者根本不使用此 Preview 类:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SurfaceView surfaceView = new SurfaceView(this);
setContentView(surfaceView);
SurfaceHolder surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
mCamera = Camera.open(0);
try {
mCamera.setPreviewDisplay(holder);
} catch (IOException exception) {
mCamera.release();
mCamera = null;
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
mCamera.startPreview();
}
});
}
关于java - Whitespace Surround Android 相机预览,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26948859/
请参阅此示例: http://jsfiddle.net/EwtaC/1/ 图像通过display:inline-block设置样式。我知道空格会以某种方式对此产生影响..(无论出于何种原因)所以我删除
我在这里设置了一个 CSS 菜单: http://pans.saebermedia.com/ 问题是,我注意到 IE 无法正确显示菜单,现在我又开始使用装有 IE8 的计算机来修复它。我正在使用兼容模
如果你尝试这样做: var x = document.createElement("div"); x.appendChild(document.createTextNode(" te
我创建了一个报表,该报表的左侧有一个图像,该图像根据传递给该报表的参数有条件地可见。页面右侧还有另一个文本框。 我观察到,当图像的“隐藏”属性设置为“True”时,右侧的文本框将保留在正确的位置。当图
Closed. This question is off-topic。它当前不接受答案。 想要改善这个问题吗? Update the question,所以它是用于堆栈溢出的on-topic。 已关闭
当我使用 ``# `` 在我的 Sphinx 文档中,我收到以下警告: WARNING: Inline literal start-string without end-string. 试 :samp
我正在尝试对 Linux 系统上具有 Windows 行结尾的文件应用补丁,但由于文件中的回车符而出现冲突。 -l 选项(忽略空格)不会忽略 EOL 字符。 有没有办法获得补丁来忽略Windows风格
您实际上如何更改vscode的“editor.tabSize”和“editor.insertSpaces”值?我打开了文件>首选项>用户设置,并添加了: // Place your settings
有谁知道一个命令行 exe/工具可以 可靠 从 C# 语言的行尾修剪空格? 最佳答案 它必须是命令行吗? Visual Studio 会在你做 Format Document 时( Ctrl+E+D
尝试创建一个背景色为白色的内容框。它位于页眉 div 和页脚 div 之间,它们都是图像。我无法让它与两个 div 对齐并且没有空白,只有一个或另一个。 这是 CSS: #content {
我想要对空白进行精细控制,但仍然有可读的模板。 只是想通过简单的用例看看其他人的解决方案。 {{name}} {{#if age}} , {{age}} {{/if}} # outputs {{n
如果我在Atom设置中选择Show Invisibles,则所有不可见的字符都变为可见: 我想隐藏EOL,因为它们会污染 View 。 这是可以实现的吗? 最佳答案 没有ui选项,但是可以通过Atom
我知道之前已经有人问过如何用前导空格分割字符串的问题,例如: String str = " I want to be split \t!" String[] sarr = str.split("\\
我不确定 slider 顶部和底部的额外空白来自哪里。是填充还是边距?如果有人可以帮助我,我将不胜感激。 jsfiddle.net/fH3EL 最佳答案 这是因为 bx-slider CSS 文件造成
我有一种自定义脚本语言,我正在尝试使用 XTEXT 进行语法检查。它归结为格式为单行命令 COMMAND:PARAMETERS 在大多数情况下,xtext 运行良好。我目前遇到的唯一问题是如何处理需要
我有一个包含超过 1,000,000 个条目的数据库,其中一些在值的开头/结尾包含空格字符。 我已经尝试了以下查询并且它有效,但我将不得不检查 1,000,000 条记录,因为所有 ID 都是唯一的
Perl 6 清除了其前身中的一些奇怪情况,不允许在某些地方使用空格,但在其他地方也执行工作。空间在哪里重要?有一个完整的引用会很好,因此我添加了一个社区 wiki 答案,我会用你的代表答案来更新。示
我想知道有什么方法可以在 Doxygen 的 html 的评论中插入空格吗?我在网上和 Doxygen 手册上搜索过,但找不到任何可以做到这一点的东西。 例如,我试图添加评论如下: //! mot
有人可以澄清一下空格在 Perl 6 语法中的规则中什么时候很重要吗?我通过反复试验学习了一些,但似乎无法在文档中找到实际规则。 示例 1: rule number { \d '.'? \d
我有一个代码库,由于尾随空格而使我发疯。我想把它清理干净。 我想: 删除所有尾随空格 删除文件末尾的所有换行符 将所有行尾转换为 unix (dos2unix) 将所有前导空格转换为制表符,即将 4
我是一名优秀的程序员,十分优秀!