gpt4 book ai didi

android - 拍照时在相机上绘制文字

转载 作者:行者123 更新时间:2023-11-29 01:19:13 25 4
gpt4 key购买 nike

我使用 this 开发了一个安卓相机应用程序tutorials点教程
我的目的是在打开应用程序时输入一个文本,我希望在拍摄图像时显示该文本
我在拍摄时使用表面 View 来显示图像预览,但现在我想在拍摄图像时在屏幕上显示文本(实时)
this答案似乎是在捕获图像后绘制文本,即在保存图像时,但我想在拍照时显示文本
文本的位置应该固定
是的,我想在保存时将该文本保存在图像中,即我不想只显示文本,但它也应该保存在图像中
我没有发布任何代码,因为它与链接中的相同,但我会在需要时更新问题
谢谢。

代码(MainActivity.java):

  package com.campg.sonix.campgs;

import android.content.res.Configuration;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;

import android.hardware.SensorManager;
import android.media.ExifInterface;
import android.os.BatteryManager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;

import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.OrientationEventListener;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;


import android.widget.Toast;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import java.util.ArrayList;
import java.util.Set;


public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback {
Camera camera;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
OrientationEventListener orientationEventListener;
Camera.PictureCallback rawCallback;
Camera.ShutterCallback shutterCallback;
Camera.PictureCallback jpegCallback;
private Camera.Size csize;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
orientationEventListener
= new OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL){

@Override
public void onOrientationChanged(int orientation) {
// TODO Auto-generated method stub
try {
if (orientation == ORIENTATION_UNKNOWN) return;
android.hardware.Camera.CameraInfo info =
new android.hardware.Camera.CameraInfo();
android.hardware.Camera.getCameraInfo(i, info);
orientation = (orientation + 45) / 90 * 90;
int rotation = 0;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
rotation = (info.orientation - orientation + 360) % 360;
} else { // back-facing camera
rotation = (info.orientation + orientation) % 360;
}
Camera.Parameters mParameters;
mParameters = camera.getParameters();
mParameters.setRotation(rotation);

Log.d("orientation is",String.valueOf(orientation));

}
catch (Exception e)
{
Log.e("orien exc", "exception", e);

}

}};

if (orientationEventListener.canDetectOrientation()){
Toast.makeText(this, "Can DetectOrientation", Toast.LENGTH_LONG).show();
orientationEventListener.enable();
}
else{
Toast.makeText(this, "Can't DetectOrientation", Toast.LENGTH_LONG).show();
finish();
}
Log.i("oncre","ate");
surfaceView = (SurfaceView) findViewById(R.id.surfaceView1);

surfaceHolder = surfaceView.getHolder();
Log.i("ya", "ya");

surfaceHolder.addCallback(this);
Log.i("ya1", "ya2");

surfaceHolder.setType(surfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
Log.i("ya3", "ya4");

jpegCallback = new PictureCallback() {

@Override
public void onPictureTaken(byte[] data, Camera camera) {
FileOutputStream outputStream =null;

try {
outputStream = new FileOutputStream(String.format("/sdcard/%d.jpg", System.currentTimeMillis()));
outputStream.write(data);
outputStream.close();
Log.i("yaha1", "yaha1");

Log.d("Log", "onPictureTaken - wrote bytes: " + data.length);
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
Log.e("hii2", "exception", e);
e.printStackTrace();
} finally {
}
Toast.makeText(getApplicationContext(), "Saved", Toast.LENGTH_LONG).show();
refreshCamera();
}
};
}

public void captureImage(View v) throws IOException {
try {
camera.takePicture(null, null, jpegCallback);

}
catch (NullPointerException e)
{
Log.i("m here", "m here");

Log.e("hey br", "exception", e);

}
catch (IllegalStateException e)
{
Log.e("hii 12 4", "exception", e);

}
catch (RuntimeException e)
{
Log.e("hii 12 4", "exception", e);

}

}

public void refreshCamera()
{
if(surfaceHolder.getSurface() == null)
return;
try
{
camera.stopPreview();
}
catch (Exception e)
{
}
try
{
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
}
catch (IOException e)
{
e.printStackTrace();
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
return true;
}
@Override
protected void onDestroy()
{
super.onDestroy();
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
return super.onOptionsItemSelected(item);
}
public static void setCameraDisplayOrientation(MainActivity activity,
int cameraId, Camera camera) {
try
{
Log.i("inside", "inside");

android.hardware.Camera.CameraInfo info =
new android.hardware.Camera.CameraInfo();
android.hardware.Camera.getCameraInfo(cameraId, 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;
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;
}
camera.setDisplayOrientation(result);
}
catch (Exception e) {
Log.e("inside", "exception", e);
}
}



int i;

@Override
public void surfaceCreated(SurfaceHolder holder)
{

try {
camera=Camera.open(i);
camera.setDisplayOrientation(180);

Log.i("m here", "m here");

}
catch (RuntimeException e) {
System.err.println(e);
Log.e("hey", "exception", e);

return;
}
Log.i("aftr", "aftr");
try
{
Camera.Parameters param;
param = camera.getParameters();
String currentversion = android.os.Build.VERSION.SDK;
Log.d("System out", "currentVersion " + currentversion);
int currentInt = android.os.Build.VERSION.SDK_INT;
Log.d("System out", "currentVersion " + currentInt);
param.set("orientation", "portrait");

if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
if (currentInt != 7) {
camera.setDisplayOrientation(90);
} else {
Log.d("System out", "Portrait " + currentInt);

param.setRotation(90);


param.set("orientation", "portrait");
param.set("rotation",90);

camera.setParameters(param);
}
}
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
// camera.setDisplayOrientation(0);
if (currentInt != 7) {
camera.setDisplayOrientation(0);
} else {
Log.d("System out", "Landscape " + currentInt);
param.set("orientation", "landscape");
param.set("rotation", 90);
camera.setParameters(param);
}
}

param.setPreviewSize(288,352 );
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
Toast.makeText(getApplicationContext(), "hi 2", Toast.LENGTH_LONG).show();
}
catch (Exception e) {
System.err.println(e);
return;
}

}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
{

try
{
setCameraDisplayOrientation(this,i,camera);
refreshCamera();
Log.i("inside sfc", "inside sfc");

}
catch (Exception e) {
Log.e("surfacechan", "exception", e);
System.err.println(e);
return;
}
}

@Override
public void surfaceDestroyed(SurfaceHolder holder)
{
try {
camera.stopPreview();
camera.release();
camera = null;
}
catch (Exception e){
System.err.println(e);
return;
}

}}


list :

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.campg.sonix.campgs"
android:versionCode="1"
android:versionName="1.0">

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"

android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

XML 文件:-

<SurfaceView
android:id="@+id/surfaceView1"
android:layout_width="match_parent"
android:layout_margin="3dp"
android:layout_height= "0dp"
android:layout_weight="1" />

<LinearLayout
android:id="@+id/capture"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="captureImage"
android:clickable="true"
android:orientation="vertical"
android:gravity="center"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp"
android:text="Capture"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>

最佳答案

用于在预览中添加文本,

1)扩展Surfaceview类

2) 通过覆盖 onDraw() 调用添加文本(或按钮等)

例如,创建一个新类 CameraOverlaySurfaceView(而不是代码中的 SurfaceView surfaceView;):

public class CameraOverlaySurfaceView extends SurfaceView {
public CameraOverlaySurfaceView(Context ctx) { /* Do init */
super(ctx);
setWillNotDraw(false);
}

@Override
protected void onDraw(Canvas canv) {
/* Draw text like in
http://stackoverflow.com/questions/2655402/android-canvas-drawtext
*/
}

用于将文本与捕获的图像一起保存为文件,

1)在jpegcallback中,需要手动在需要的位置添加Text位图,重新编码存储。

关于android - 拍照时在相机上绘制文字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37909777/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com