赋料扬雄敌,诗看子建亲。这篇文章主要讲述如何在Android中自定义相机布局?相关的知识,希望能为你提供帮助。
我正在尝试使用文本识别功能开发某种OCR应用程序。我写了并找到了一些正常工作的代码,但我的问题是我想在相机布局中进行一些自定义。我想添加自己的捕获按钮并添加一个框架。我实际上是在“表面视图/持有者”的不同项目中完成的。但我无法实现我的项目,因为它的工作方式不同。
public class MainActivity extends AppCompatActivity {private static final int REQUEST_GALLERY = 0;
private static final int REQUEST_CAMERA = 1;
private static final String TAG = MainActivity.class.getSimpleName();
private Uri imageUri;
private TextView detectedTextView;
// layouttaki text view@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.choose_from_gallery).setOnClickListener(new View.OnClickListener() {// galeriden resim se?me i?lemi
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, REQUEST_GALLERY);
}
});
findViewById(R.id.take_a_photo).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {// resim ?ekme i?lemiString filename = System.currentTimeMillis() + ".jpg";
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, filename);
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
imageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent intent = new Intent();
intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, REQUEST_CAMERA);
}
});
detectedTextView = (TextView) findViewById(R.id.detected_text);
detectedTextView.setMovementMethod(new ScrollingMovementMethod());
}private void inspectFromBitmap(Bitmap bitmap) {//kendisine gelen bitmap resimden inspect yaparTextRecognizer textRecognizer = new TextRecognizer.Builder(this).build();
try {
if (!textRecognizer.isOperational()) {
new AlertDialog.
Builder(this).
setMessage("Text recognizer could not be set up on your device").show();
return;
}Frame frame = new Frame.Builder().setBitmap(bitmap).build();
SparseArray<
TextBlock>
origTextBlocks = textRecognizer.detect(frame);
List<
TextBlock>
textBlocks = new ArrayList<
>
();
for (int i = 0;
i <
origTextBlocks.size();
i++) {
TextBlock textBlock = origTextBlocks.valueAt(i);
textBlocks.add(textBlock);
}
Collections.sort(textBlocks, new Comparator<
TextBlock>
() {
@Override
public int compare(TextBlock o1, TextBlock o2) {
int diffOfTops = o1.getBoundingBox().top - o2.getBoundingBox().top;
int diffOfLefts = o1.getBoundingBox().left - o2.getBoundingBox().left;
if (diffOfTops != 0) {
return diffOfTops;
}
return diffOfLefts;
}
});
StringBuilder detectedText = new StringBuilder();
for (TextBlock textBlock : textBlocks) {
if (textBlock != null &
&
textBlock.getValue() != null) {
detectedText.append(textBlock.getValue());
detectedText.append("
");
}
}detectedTextView.setText(detectedText);
// detectedText is a final string
}
finally {
textRecognizer.release();
}
}private void inspect(Uri uri) {
InputStream is = null;
Bitmap bitmap = null;
try {
is = getContentResolver().openInputStream(uri);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
options.inSampleSize = 2;
options.inScreenDensity = DisplayMetrics.DENSITY_LOW;
bitmap = BitmapFactory.decodeStream(is, null, options);
Bitmap rotatedMap = RotateBitmap(bitmap,90);
inspectFromBitmap(rotatedMap);
} catch (FileNotFoundException e) {
Log.w(TAG, "Failed to find the file: " + uri, e);
} finally {
if (bitmap != null) {
bitmap.recycle();
}
if (is != null) {
try {
is.close();
} catch (IOException e) {
Log.w(TAG, "Failed to close InputStream", e);
}
}
}
}@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_GALLERY:
if (resultCode == RESULT_OK) {
inspect(data.getData());
}
break;
case REQUEST_CAMERA:
if (resultCode == RESULT_OK) {
if (imageUri != null) {
inspect(imageUri);
}
}
break;
default:
super.onActivityResult(requestCode, resultCode, data);
break;
}
}public static Bitmap RotateBitmap(Bitmap source, float angle) // it rotates the bitmap for given parameter
{
Matrix matrix = new Matrix();
matrix.postRotate(angle);
return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
}
【如何在Android中自定义相机布局()】在那种情况下,我该怎么办?感谢你们。
答案不,您无法更改满足ACTION_IMAGE_CAPTURE意图的相机应用程序的布局。实际上,不同的设备不会有相同的相机应用程序。每个人可能会有非常不同的外观。您需要一个“自定义摄像头”来控制其布局和UX。
推荐阅读
- Android(需要格式化布局的帮助)
- android中的屏幕兼容性问题
- 在android加载布局时出现了一些错误,但在运行时没有效果
- Android(我应用中所有Spinners的全局风格)
- 将Visual Studio 2012附加到在Android移动设备上运行的Unity游戏中,以进行调试
- 在android studio中build.gradle中使用flavor维度
- 如何调试Google Apps脚本(又名Logger.log登录到哪里())
- 如何解决TCPDF错误(一些数据已经输出,无法发送PDF文件)
- 如何在Windows中配置NetBeans以将LESS文件自动编译为CSS